1 # needs to traverse all of the files and subfolders subfolders - a recursive 2 . 3 '' '' '' 4 '' 5 pseudocode . 6 1, the root directory traversal for --listdir . 7 to obtain a first stage subfolders (subfolders not contain subfolders) and file 8 2, the judgment is a file or folder 9 if the file directly print the file name 10 if it is a folder (full path for the job), it calls itself (recursively) . 11 12 is step: 13 is 1 to realize the function 14 2, and then optimize - a directory of each print indentation 15 '' ' 16 Import OS . 17 18 is # path1 = r'D: \ Program \ JetBrains \ PycharmProjects \ xq_py \ full stack 16 \ 001 '# absolute path . 19 path1 = R & lt ' ..\001' # Relative path, .. \ indicates the parent directory of the current directory \ represents the current directory # recommended. 20 is 21 is DEF for_file (path1, n-): # Parameter 1 is the root directory path of the folder, the parameter for adjusting the level 2 indentation 22 is in Li1 = the os.listdir (path1) # . 1 lists the files under the root directory of the first folder hierarchy (which does not include sub-folders and sub-files) and returns a list of 23 is # Print (in Li1) # [ '002 ',' 003.py ',' 005.py ',' __init__.py '] 24 for I in in Li1: # 2 through the loop list, printing the first folder and file hierarchy 25 full_path1 = the os.path.join (path1, I) # . 3 spliced full path of the first folder hierarchy 26 is # Print (full_path1) 27 iF os.path.isdir (full_path1): # . 4 determines whether the full path to the folder 28 Print ( ' \ t ' * + the n-i) # 4-2 is the full path to the folder, then print the name of the folder (the folder name do not need to print the full path) 29 for_file (full_path1, the n-+ 1) # own tune # 5 is the full path to your own folders, it calls itself - recursion 30 # the first level folder as the root directory, 31 # Note: recursion when the folder must be a full path, not a folder name 32 # parameter 2 n + 1 represents the time each recursion, indentation will increase. 1 33 is the else : # . 6 full path is not a folder, then, is the name of the file, the file can be printed (print name of the file does not need the full path) 34 is Print ( ' \ T ' * + n-I) # 6-2 '\ T' represents a tab-delimited spaces indentation -4 35 36 for_file (path1,1) #7 call the function, the parameter is a root folder full path (which may be an absolute path or a relative path - Recommended) 37 # parameter 2 is used to adjust the level of indentation 38 # file in the root folder (containing hierarchy its subfolders and sub-files) and FIG indentation 39 # 002 40 # 003 41 is # 004.py 42 is # __init__.py 43 is # 003.py 44 is # __init__.py 45 # 003.py 46 is # 005.py 47 # __init__.py