Last active
February 28, 2016 04:47
-
-
Save robperc/102463c8311cbe4da3b7 to your computer and use it in GitHub Desktop.
Re-implementation of os.walk as a recursive generator
This file contains hidden or 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
""" | |
Re-implementation of os.walk as a recursive generator. | |
Yields full paths of files encountered along the walk of the parent directory. | |
""" | |
import os | |
def walkDir(parent): | |
""" | |
Walks parent directory yielding files along the way. | |
Args: | |
parent (str): Path of parent directory to begin walk at. | |
Yields: | |
Paths of files encountered along the way. | |
""" | |
contents = os.listdir(parent) | |
for node in contents: | |
path = os.path.join(parent, node) | |
if os.path.isdir(path): | |
for sub_dir in walkDir(path): | |
yield sub_dir | |
else: | |
yield path | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment