An extremely simple tree-view generator for a directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import os import sys def create_tree_structure(rootpath): rootpath = rootpath.rstrip(os.sep) start_level = rootpath.count(os.sep) for root, dirs, files in os.walk(rootpath): present_level = root.count(os.sep) actual_level = present_level - start_level spacing = (actual_level) * ' ' file_list = [file for file in files] sys.stdout.write(spacing + '-' + os.path.basename(root) + ' ' + str(file_list) + '\n') if __name__ == '__main__': create_tree_structure('../path')
I am a python newbie and I am lately learning a lot of things. I had wanted a tree view of my own. I did find some code in the net. But I really wanted to keep things simple. The output generated is quite useful for me, but again, there is room for lot of improvement.
Suggestions,tips, improvements are welkom.