This article describes the method of directory traversal of Python file operations. Share to everyone for your reference. The specific analysis is as follows:
Python's OS module, which contains common operating system functions, mainly learns path-related functions:
Os.listdir (dirname): List directories and files under DirName
OS.GETCWD (): Get current working directory
Os.curdir: Returns the current directory ('. ')
Os.chdir (dirname): Changing the working directory to DirName
Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false
Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false
Os.path.exists (name): Determine if there is a file or directory name
Os.path.getsize (name): Gets the file size if name is directory returned 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 file name, and it will not determine whether the file or directory exists)
Os.path.splitext (): Detach file name and extension
Os.path.join (path,name): Connection directory with file name or directory
Os.path.basename (PATH): Return file name
Os.path.dirname (PATH): Return 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 number of files, the OS-provided path module can operate very flexibly on the directory.
Import Os,sysdef Listdir (dir,file): file.write (dir + ' \ n ') fielnum = 0 list = Os.listdir (dir) # List all files and directories in the directory for line in list: filepath = os.path.join (dir,line) if Os.path.isdir (filepath): # If FilePath is a directory, then list all files in that directory myfile.write (' + line + ' \ \ ' + ' \ n ') for Li in Os.listdir (filepath): Myfile.write (' +li + ' \ n ') fielnum = fielnum + 1 elif os.path: #如果filepath是文件, list file names directly 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 x under directory list], directory x below the file)
Import Osdef Walk_dir (dir,fileinfo,topdown=true): for Root, dirs, files in Os.walk (dir, topdown): for name in fil ES: print (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, if Topdown is true, first enumerates the directories under top, then the directory, and so on, and vice versa, recursively enumerates the deepest subdirectories, then their siblings, and then subdirectories.
Hopefully this article will help you with Python programming.