From:http://zhidao.baidu.com/link?url=O8U5TynGBMojDw2iFhlghPPf5_ZE1X8CAQMrK19pv-KxhvKCc6Z2yzsoQaukgN2haYudVO7Hg3c0ZvNm8ALcY_
#!/usr/bin/python3
# -*- coding: utf- -*- def print_tree(tree):
buff = ['ROOT/']
_print_tree(tree, buff, '', )
print('\n'.join(buff)) def _print_tree(tree, buff, prefix, level):
count = len(tree)
for k, v in tree.items():
count -=
if v:
buff.append('%s +- %s/' % (prefix, k))
if count > :
_print_tree(v, buff, prefix + ' | ', level + )
else:
_print_tree(v, buff, prefix + ' ', level + )
else:
buff.append('%s +- %s' % (prefix, k)) def test():
tree = {
'bin': { 'bash': None, 'cat': None, 'cp': None, },
'etc': {
'init.d': { 'apache2':None, 'slapd':None, 'sshd':None, },
'passwd': None,
'hosts': None,
},
'var': {
'log': {
'apache2': { 'accesslog':None, 'errorlog': None, },
},
},
}
print_tree(tree) if __name__ == '__main__':
test()
输出
ROOT/
+- etc/
| +- passwd
| +- init.d/
| | +- apache2
| | +- sshd
| | +- slapd
| +- hosts
+- bin/
| +- cp
| +- bash
| +- cat
+- var/
+- log/
+- apache2/
+- errorlog
+- accesslog