This article mainly introduces three methods for traversing objects in python. path. walk (), OS. walk (), OS. listdir () is implemented. if you need it, you can refer to writing a python script that batch modifies file names in windows and use file traversal. There are multiple methods to traverse objects using python.
OS. path. walk ()
This is a traditional usage.
The walk (root, callable, args) method has three parameters: the directory to be traversed, the callback function, and the callback function parameters (in the form of tuples ).
The call process is to traverse the files or directories under the Directory. every time a directory is traversed, the callback function is called and the args is passed as a parameter to the callback function.
The callback function also has three parameters. for example, the three parameters in func in the example are the parameters sent from the walk, the directory path, and the file list under the Directory (only the file name, is not the complete path ). See the example:
The code is as follows:
Import OS
S = OS. sep # according to unix or win, s is \ or/
Root = "d:" + s + "ll" + s # directory to be traversed
Def func (args, dire, FIIs): # callback function definition
For f in FCM:
Fname = OS. path. splitext (f) # Split the file name into a binary group with the name and extension.
New = fname [0] + 'B' + fname [1] # Rename
OS. rename (OS. path. join (dire, f), OS. path. join (dire, new) # rename
OS. path. walk (root, func, () # Traverse
There is a problem when using this method. you cannot recursively repeat the next layer (I am not sure about this. please correct me ).
OS. walk () is added to the advanced version of python, which is better than this.
OS. walk ()
Prototype: OS. walk (top, topdown = True, onerror = None, followlinks = False)
We generally only use the first parameter. (Topdown indicates the traversal order)
This method returns a triple (dirpath, dirnames, filenames) for each directory ). The first is the path, the second is the directory under the path, and the third is the non-directory under the path (for windows, that is, the file ). See the example:
The code is as follows:
Import OS
S = OS. sep
Root = "d:" + s + "ll" + s
For rt, dirs, files in OS. walk (root ):
For f in files:
Fname = OS. path. splitext (f)
New = fname [0] + 'B' + fname [1]
OS. rename (OS. path. join (rt, f), OS. path. join (rt, new ))
This method recursively traverses all files.
Listdir
You can combine several methods in the OS module for traversal. See the example:
The code is as follows:
Import OS
S = OS. sep
Root = "d:" + s + "ll" + s
For I in OS. listdir (root ):
If OS. path. isfile (OS. path. join (root, I )):
Print I
Note that I is a directory or file name, not a complete path. you must use the OS. path. join () method to restore the complete path.
After the traversal is done, you can use regular expressions to perform advanced processing for file name modification.
In addition, you can also use OS. system (cmd) to call commands in shell to process files, which is very powerful.