Skip to content

Instantly share code, notes, and snippets.

@mbarkhau
Last active December 15, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbarkhau/5192023 to your computer and use it in GitHub Desktop.
Save mbarkhau/5192023 to your computer and use it in GitHub Desktop.
Iterate over all filenames in a directory tree
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