Created
September 25, 2012 21:18
-
-
Save gkalab-snippets/3784511 to your computer and use it in GitHub Desktop.
Python: dirwalk
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
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