Os.walk (Top,topdown = True,onerror = None,followlinks = False)
Parameters
Top -Every folder in the root directory (including itself), resulting in 3-tuple (Dirpath, dirnames, filenames) "Folder path, folder name, file name".
Topdown --optional, true or unspecified, a 3-tuple of a directory will be generated first (directory top-down) than the 3-tuple of any of its subfolders. If Topdown is False, a 3-tuple of a directory will be generated after the 3-tuple of any of its subfolders (directory bottom-up).
onerror --optional, is a function; It is called when there is a parameter, an OSError instance. After reporting this error, continue to walk, or throw exception to terminate walk.
followlinks --set to True to access the directory through a soft link.
return value
The method has no return value.
Instance
#-*-Coding:utf-8-*-#import osimport os.pathfor root, dirs, files in Os.walk ("D:\Leo\cew", Topdown=false): for name In Files: print ( name) print ('-' *10) for name in dirs: print ( name) print (' # ' *10)
Execution results
2.txt
----------
ew
##########
New text document. txt
----------
1
##########
2
##########
Querying with the tree command
Tree d:\leo\cewd:\leo\cew├─ 1 └─ 2 └─ew
Python Learning Os.walk ()