Um alle Dateien eines Verzeichnisses und/oder Unterverzeichnisse zu erhalten, verwende ich den folgenden Code in vielen meiner Projekte:
import os
def get_all_files(rootdir, mindepth = 1, maxdepth = float('inf')):
"""
Usage:
d = get_all_files(rootdir, mindepth = 1, maxdepth = 2)
This returns a list of all files of a directory, including all files in
subdirectories. Full paths are returned.
WARNING: this may create a very large list if many files exists in the
directory and subdirectories. Make sure you set the maxdepth appropriately.
rootdir = existing directory to start
mindepth = int: the level to start, 1 is start at root dir, 2 is start
at the sub direcories of the root dir, and-so-on-so-forth.
maxdepth = int: the level which to report to. Example, if you only want
in the files of the sub directories of the root dir,
set mindepth = 2 and maxdepth = 2. If you only want the files
of the root dir itself, set mindepth = 1 and maxdepth = 1
"""
rootdir = os.path.normcase(rootdir)
file_paths = []
root_depth = rootdir.rstrip(os.path.sep).count(os.path.sep) - 1
for dirpath, dirs, files in os.walk(rootdir):
depth = dirpath.count(os.path.sep) - root_depth
if mindepth <= depth <= maxdepth:
for filename in files:
file_paths.append(os.path.join(dirpath, filename))
elif depth > maxdepth:
del dirs[:]
return file_paths
Danach können Sie filtern, schneiden und würfeln, wie auch immer Sie wollen. Wenn Sie innerhalb dieser Routine filtern möchten, ist der Ort, um es zu tun, nach for filename in files:
und vor file_paths.append(os.path.join(dirpath, filename))
HTH.