Skip to content

Instantly share code, notes, and snippets.

@robperc
Last active February 28, 2016 04:47
Show Gist options
  • Save robperc/102463c8311cbe4da3b7 to your computer and use it in GitHub Desktop.
Save robperc/102463c8311cbe4da3b7 to your computer and use it in GitHub Desktop.
Re-implementation of os.walk as a recursive generator
"""
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