Created
October 31, 2015 17:22
-
-
Save discipolo/403600a1c1fe691c39ba to your computer and use it in GitHub Desktop.
stateful directory list
This file contains 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
#!/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