Nec*_*tar 0 python tree ascii python-2.7
是否可以使用Python(或某种lib)生成目录及其所有子目录+文件的基于ascii的树结构?
我已经尝试了很多方法,但不幸的是我无法解决这个问题。
输出的示例如下所示:
[rootdir]
|
+--- [subdir0]
|
+--- [subdir1]
| |
| +--- file1
| +--- file2
|
+--- [subdir2]
| |
| +--- [subdir3]
| |
| +--- [subdir4]
| |
| +--- [subdir5]
| |
| +--- [subdir6]
| | |
| | +--- file4
| |
| +--- file3
+--- file5
+--- file6
Run Code Online (Sandbox Code Playgroud)
编辑:
我当前的(很棒的)剧本被要求了。
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))
Run Code Online (Sandbox Code Playgroud)