一、一级 Directory
' d: \file '; for inch os.listdir (path):p rint (Os.path.join (path,filename)) Use Os.listdir to read all the file names under a directory, and then use Os.path.join to combine the directory path and file name to get the file path, the result is as follows:
D:\file\test1.txt
D:
D:
Case 2: Recursive situation, a directory under the existing directory (directories and files may also have a directory and file) also have files, how to read all the files inside, using Os.walk:
Ii. Directories with sub-directories
Import OS
Path = R ' C:\Users\Administrator\Desktop\file '
For dirpath,dirnames,filenames in Os.walk (path):
Print (Dirpath,dirnames,filenames)
The output results are as follows:
C:\file [' file1 ', ' file2 '] [' file_test1.txt ', ' file_test2. txt ']
C:\file\file1 [] [' file1_test1.txt ', ' file1_test2.txt ']
C:\file\file2 [] [' File2_test1.txt ']
Python Traversal folder The difference between Listdir walk