Os.walk method
1 Import OS 2 for in Os.walk (R'C:\Users\jack\Desktop\test\3_ language edition seven-year book \1_ one unit '): 3 print(i[0])
The Os.walk method returns a ternary tupple (Dirpath, Dirnames, filenames),
The first one is the starting path,
The second is a folder under the starting path,
The third one is the file under the starting path.
Dirpath is a string that represents the path to the directory,
Dirnames is a list that contains the names of all subdirectories under Dirpath,
Filenames is a list that contains the name of a non-directory file.
These names do not contain path information and need to use Os.path.join (Dirpath, name) if you need to get the full path.
1. Print i[0], list the full path of each folder in the walk path.
2. Print I[1], list all the folders under the walk path, starting with the first folder, layers in depth, if there are no folders, the display is empty [].
3. Print I[2], return to the walk path, all the file names (not including the folder name), starting from the first folder, if there is no file in the folder directory, only the folder, then return empty [].
You can also directly write 3 variable names to receive the Walk return value:
1 Import OS 2 for in Os.walk (R'C:\Users\jack\Desktop\test\3_ language edition seven-year book \1_ one unit '): 3 print(dirnames)
Python walk function