Skip to content

Instantly share code, notes, and snippets.

@jomido
Last active April 3, 2017 21:03
Show Gist options
  • Save jomido/b43050345b7c78dc5c9cd0662ad677dd to your computer and use it in GitHub Desktop.
Save jomido/b43050345b7c78dc5c9cd0662ad677dd to your computer and use it in GitHub Desktop.
Recursive, max depth file walk, with filtering
always = lambda f: True
def get_filenames(root_path, max_depth=1, predicate=None):
predicate = predicate or always
path = os.path.normpath(root_path)
filenames = []
for root, dirs, files in os.walk(path, topdown=True):
depth = root[len(path):].count(os.path.sep)
if max_depth < 0 or depth < max_depth:
filenames += [os.path.join(root, f) for f in files if predicate(f)]
else:
break
return filenames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment