This example describes the method of directory traversal for Python file operations. Share to everyone for your reference. The specific analysis is as follows:
Python's OS module, which contains the universal operating system features, is mainly about path-dependent functions:
Os.listdir (dirname): Listing directories and files under DirName
OS.GETCWD (): Get current working directory
Os.curdir: Returns the current directory ('. ')
Os.chdir (dirname): Changing working directory to DirName
Os.path.isdir (name): Determines if name is a directory, name is not a directory returns false
Os.path.isfile (name): Determines whether name is a file, does not exist name also returns false
Os.path.exists (name): Determines whether a file or directory name exists
Os.path.getsize (name): Get file size, if name is directory return 0
Os.path.abspath (name): Get absolute path
Os.path.normpath (PATH): Canonical path string form
Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the filename, and it will not determine whether the file or directory exists)
Os.path.splitext (): Detach file name and extension
Os.path.join (path,name): Connecting directories with file names or directories
Os.path.basename (PATH): Return file name
Os.path.dirname (path): Back to File path
1, Os.path method
By passing in the directory that needs to be traversed, listing all the files in the directory and counting the files, the path module provided by the OS can be very flexible to the directory.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18||Import Os,sys def listdir (dir,file): file.write (dir + ' n ') fielnum = 0 list = Os.listdir (dir) #列出目录下的所有文件和目录 for line in L Ist:filepath = Os.path.join (dir,line) if Os.path.isdir (filepath): #如果filepath是目录, then list all files in the directory Myfile.write (' + line + ' + ' n ') for Li in Os.listdir (filepath): Myfile.write (' +li + ' n ') Fielnum = fielnum + 1 elif os.path: #如果filepath是文件, direct columns Out filename myfile.write (' +line + ' n ') Fielnum = fielnum + 1 myfile.write (' All of the file num is ' + str (fielnum)) dir = Raw_input (' Please input the path: ') myfile = open (' List.txt ', ' W ')|
2, Os.walk method
The walk method provided by the OS module is powerful enough to traverse all directories and files in a given directory.
Method: Os.walk (path), traverse path, return an object, each of his parts is a ternary group, (' Directory x ', [directory list under directory X], directory x below the file)
|1 2 3 4 5 6 7 8 9 10 11-12||Import os def walk_dir (dir,fileinfo,topdown=true): For root, dirs, files in Os.walk (dir, topdown): for name in Files:prin T (os.path.join (name)) Fileinfo.write (Os.path.join (root,name) + ' n ') for name in Dirs:print (Os.path.join (name)) Fileinfo.write (' + os.path.join (root,name) + ' n ') dir = raw_input (' Please input the path: ') FileInfo = open (' List.txt ', ' W ') Walk_dir (Dir,fileinfo)|
Topdown determines the order of traversal, and if Topdown is true, first enumerates the directories under top, then directories, and so on, and vice versa, recursively enumerates the deepest subdirectories, then their siblings, and then subdirectories.
I hope this article will help you with your Python programming.