Last active
December 15, 2015 03:09
-
-
Save mbarkhau/5192023 to your computer and use it in GitHub Desktop.
Iterate over all filenames in a directory tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def filepaths(rootdir, ext_filter=None): | |
if isinstance(ext_filter, basestring): | |
_filter = lambda p: p.endswith(ext_filter) | |
elif isinstance(ext_filter, tuple): | |
_filter = lambda p: os.path.splitext(p)[1] in ext_filter | |
else: | |
_filter = ext_filter | |
for root, subfolders, files in os.walk(rootdir): | |
for filename in files: | |
path = os.path.abspath(os.path.join(root, filename)) | |
if not _filter or _filter(path): | |
yield path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment