path - Listing a single file in multiple directories with python -
i have found many methods list every file in multiple directories, so:
root = "c:\\test\\" path, subdirs, files in os.walk(root): name in files: print(os.path.join(path, name))
however, need list 1 file in each directory. not looking particular order, not need randomness either. there way single file (preferably "first") in each directory save resources take list every file? (this windows filesystem, if relevant.)
try following code:
import os root = "c:\\test\\" path, subdirs, files in os.walk(root): if files: print(os.path.join(path, min(files)))
update
to exclude initial directory:
import os import itertools root = "c:\\test\\" path, subdirs, files in itertools.islice(os.walk(root), 1, none): if files: print(os.path.join(path, min(files)))
used min
first (alphabetically) filename.
Comments
Post a Comment