Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python: dirwalk
import os
def dirwalk(directory):
"walk a directory tree, using a generator"
for name in os.listdir(directory):
fullpath = os.path.join(directory, name)
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
for name in dirwalk(fullpath): # recurse into subdir
yield name
else:
yield fullpath
if __name__ == "__main__":
for file in dirwalk("c:\\tmp"):
print file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.