Skip to content

Instantly share code, notes, and snippets.

@akshaybabloo
Last active September 20, 2016 04:01
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 akshaybabloo/ba3180b2686338ff1bcd to your computer and use it in GitHub Desktop.
Save akshaybabloo/ba3180b2686338ff1bcd to your computer and use it in GitHub Desktop.
List files & folders in a tree structure and write to text file.
import os
from __future__ import print_function
def list_files(startpath):
fg = open('structure.txt', 'w+')
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
a = ('{}|-{}/'.format(indent, os.path.basename(root)))
print(a)
fg.write(a)
subindent = ' ' * 4 * (level + 1)
for f in files:
b = ('{}|-{}'.format(subindent, f))
print(b)
fg.write(b)
if __name__ == "__main__":
list_files("") # enter your folder location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment