From:http://zhidao.baidu.com/link?url=o8u5tyngbmojdw2ifhlghppf5_ Ze1x8caqmrk19pv-kxhvkcc6z2yzsoqaukgn2hayudvo7hg3c0zvnm8alcy_
#!/usr/bin/python3#-*-coding:utf-8-*-def print_tree (tree): Buff= ['root/'] _print_tree (tree, Buff,"',0) Print ('\ n'. Join (Buff)) def _print_tree (tree, buff, prefix, level): Count=len (tree) forKvinchTree.items (): Count-=1 ifV:buff.append ('%s +-%s/'%(prefix, k))ifCount >0: _print_tree (V, buff, prefix+' | ', Level +1) Else: _print_tree (V, buff, prefix+' ', Level +1) 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 ()
Output
root/+-etc/| +-| +-init.d/| | +-| | +-| | +-| +-+ bin/| +-| +-| +-var/ + log/ +-apache2/+- errorlog +-Accesslog
"Python" prints the tree with text