Last active
April 3, 2017 21:03
-
-
Save jomido/b43050345b7c78dc5c9cd0662ad677dd to your computer and use it in GitHub Desktop.
Recursive, max depth file walk, with filtering
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
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