Skip to content

Instantly share code, notes, and snippets.

@Ilgrim
Forked from gkalab-snippets/dirwalk.py
Created August 22, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ilgrim/ba0d8f080b855df5506839660c1db3b3 to your computer and use it in GitHub Desktop.
Save Ilgrim/ba0d8f080b855df5506839660c1db3b3 to your computer and use it in GitHub Desktop.
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