Last active
September 20, 2016 04:01
-
-
Save akshaybabloo/ba3180b2686338ff1bcd to your computer and use it in GitHub Desktop.
List files & folders in a tree structure and write to text file.
This file contains hidden or 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 | |
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