Skip to content

Instantly share code, notes, and snippets.

@htr3n
Last active April 22, 2019 00:57
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 htr3n/6a0bc6e461ba8cc2aa5c4c970080d5b9 to your computer and use it in GitHub Desktop.
Save htr3n/6a0bc6e461ba8cc2aa5c4c970080d5b9 to your computer and use it in GitHub Desktop.
Python -- traverse a folder
import os
for dirname, dirnames, filenames in os.walk('.'):
# print path to all subdirectories first.
for subdirname in dirnames:
print(os.path.join(dirname, subdirname))
# print path to all filenames.
for filename in filenames:
print(os.path.join(dirname, filename))
# Advanced usage:
# editing the 'dirnames' list will stop os.walk() from recursing into there.
if '.git' in dirnames:
# don't go into any .git directories.
dirnames.remove('.git')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment