Skip to content

Instantly share code, notes, and snippets.

@discipolo
Created October 31, 2015 17:22
Show Gist options
  • Save discipolo/403600a1c1fe691c39ba to your computer and use it in GitHub Desktop.
Save discipolo/403600a1c1fe691c39ba to your computer and use it in GitHub Desktop.
stateful directory list
#!/usr/bin/python
import os
import sys
tree = []
def main(argv):
# extract path to an attachment directory (remove *.txt extension)
path = argv[1]
# extract page name from a path
page = str(path.split("/")[-1:][0])
# construct output directory (deprecated)
path = "%s" % (path)
targetfile = os.path.join(path, 'index.txt')
showFolderTree(path,show_files=True,indentation=4,file_output=(targetfile))
def showFolderTree(path,show_files=False,indentation=2,file_output=False):
"""
Shows the content of a folder in a tree structure.
path -(string)- path of the root folder we want to show.
show_files -(boolean)- Whether or not we want to see files listed.
Defaults to False.
indentation -(int)- Indentation we want to use, defaults to 2.
file_output -(string)- Path (including the name) of the file where we want
to save the tree.
"""
tree = []
if not show_files:
for root, dirs, files in os.walk(path):
level = root.replace(path, '').count(os.sep)
indent = ' '*indentation*(level)
tree.append('{}{}/'.format(indent,os.path.basename(root)))
if show_files:
for root, dirs, files in os.walk(path):
level = root.replace(path, '').count(os.sep)
indent = ' '*indentation*(level)
tree.append('{}{}/'.format(indent,os.path.basename(root)))
for f in files:
subindent=' ' * indentation * (level+1)
tree.append('{}{}'.format(subindent,f))
if file_output:
output_file = open(file_output,'w')
for line in tree:
output_file.write(line)
output_file.write("\n")
else:
# Default behaviour: print on screen.
for line in tree:
print (line)
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment