Is it possible using Python (or some kind of lib) to generate an ascii based tree structure of a directory and all its subdirectories + files?
I've tried a bunch of thing but unfortunately I have not been able to solve this problem.
An example of the output would look something like this:
[rootdir]
|
+--- [subdir0]
|
+--- [subdir1]
| |
| +--- file1
| +--- file2
|
+--- [subdir2]
| |
| +--- [subdir3]
| |
| +--- [subdir4]
| |
| +--- [subdir5]
| |
| +--- [subdir6]
| | |
| | +--- file4
| |
| +--- file3
+--- file5
+--- file6
Edit:
My current (aweful) script was requested.
def treeStructure(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 2 * (level)
print('{}|'.format(indent[:]))
print('{}+{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 2 * (level + 1)
for f in files:
print('{}| +--- {}'.format(subindent[:-2], f))