Skip to content

Instantly share code, notes, and snippets.

@zkendall
Last active August 29, 2015 13:56
Show Gist options
  • Save zkendall/d0fff49e50ea419179d7 to your computer and use it in GitHub Desktop.
Save zkendall/d0fff49e50ea419179d7 to your computer and use it in GitHub Desktop.
Print out the directory tree with indentation from a starting path.
# Taken from: http://stackoverflow.com/a/9728478/768671
import os
def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print('{}{}'.format(subindent, f))
## SAMPLE OUTPUT ##
# webapp/
# emailer.py
# views.py
# __init__.py
# auth/
# user.py
# users.py
# __init__.py
# static/
# base.css
# index.js
# jquery-2.0.3.js
# jquery.tablesorter.js
# results.css
# results.js
# scrape.css
# scrape.js
# templates/
# base.html
# demo.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment