Skip to content

Instantly share code, notes, and snippets.

@icarofreire
Created August 28, 2018 01:23
Show Gist options
  • Select an option

  • Save icarofreire/9278ebb3f5a9a531bbf92a3f98f14a2b to your computer and use it in GitHub Desktop.

Select an option

Save icarofreire/9278ebb3f5a9a531bbf92a3f98f14a2b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: latin1 -*-
"""
"""
import os
import shutil
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)
showFolderTree("D:\\Documentos\\editor-gtk-c",True,6,"D:\\Documentos\\editor-gtk-c\\textedit\\tree.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment