Alle Dateien eines Datenformats anzeigen.
Verfasst: Montag 6. Mai 2013, 21:30
Ich möchte eine Liste mit allen Dateien, auf meinem Rechner, eines beliebigen ausgewählten Datenformats (jpg, pdf) erstellen. Welche Module brauche ich dafür?
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Nichts außer der Standardbibliothek. Grundsätzlich klingt das nach einer Aufgabe für os.walk.MikeDee hat geschrieben:Ich möchte eine Liste mit allen Dateien, auf meinem Rechner, eines beliebigen ausgewählten Datenformats (jpg, pdf) erstellen. Welche Module brauche ich dafür?
JohnDoe hat geschrieben:Oder kann das os.walk() auch und ich habs nur übersehen?
Python Doku hat geschrieben: If optional argument topdown is True or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). If topdown is False, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up).
When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.
Das klingt für mich nicht gerade einfach, kannst du mir ein praktisches Beispiel geben wie man das effektiv anwendet? Zum Vergleich eine einfache Suche die alle jpgs auflistet, inklusive Begrenzung für die Suchtiefe.Hyperion hat geschrieben:Python Doku hat geschrieben:If optional argument topdown is True or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). If topdown is False, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up).
When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
path = '/home/username'
level = 5 # Sucht aktuell 4 Ebenen tief, mit -1 wird gesucht was das Zeug hält :)
filter = '.jpg'
def search(path, level, filter):
local_list = []
if os.path.islink(path):
print('Link: %s' % path)
elif level > 0 or level < 0:
directorylist = os.listdir(path)
for element in directorylist:
if os.path.isdir(path + os.sep + element):
newpath = path + os.sep + element
newlevel = level - 1
search(newpath, newlevel, filter)
else:
newpath = path + os.sep + element
if newpath.endswith(filter):
print(newpath)
if __name__ == '__main__':
search(path, level, filter)
Wow, das ist tatsächlich einfach und elegant gelöst.MikeDee hat geschrieben:Wie wäre es damit? http://www.pythonforbeginners.com/syste ... in-python/