The idea is relatively simple, using Os.walk to generate directory information, and then the string operation, so there is no other complex logic, but the resulting effect is relatively simple, see for yourself can ha.
The code is as follows
#-*-Coding:utf-8-*-"Created on July, 2017@author:hehe1234567" Import osdef simple_dir_tree (Ddir): For dir Path,dirnames,filenames in Os.walk (Ddir.strip (OS.SEP)): if Dirpath = = Ddir: string = "for f in filenames:<" c4/>string = string + ' \ n ' + ' *len (dirpath) + ' |__ ' + str (f) print Ddir +string else: Dirn = Os.path.basenam E (dirpath) string = "*len (os.path.dirname (dirpath)) + ' |__ ' + Dirn for f in filenames: string = string + ' \ N ' + ' *len (dirpath) + ' |__ ' + str (f) print stringif __name__ = = ' __main__ ': simple_dir_tree (' C:\\users\\auser . Mey\\desktop\\loadbalance-playbook\\loadbalance-playbook ')
The resulting directory tree is as follows:
Python "Building a directory tree with simple Os.walk"