Three methods for traversing a file in python
- 2020-04-02 14:01:34
- OfStack
Today, I wrote a python script to change the file name in batches under Windows, using the traversal of the file. There are several ways to do file traversal in python, which are listed and explained here.
OS. The path. The walk ()
This is a traditional use.
The walk(root,callable,args) method takes three arguments: the directory to traverse, the callback function, and the arguments to the callback function (in tuple form).
The procedure is to traverse the files or directories under the directory, one directory at a time, call the callback function, and pass the args as an argument to the callback function.
The callback function is also defined with three parameters, such as the three parameters in func in the example, which are the parameters from walk, the path to the directory, and the list of files in the directory (only the file name, not the full path). See an example:
import os
s = os.sep # According to the unix or win . s For the or /
root = "d:" + s + "ll" + s # The directory to traverse
def func(args,dire,fis): # The definition of a callback function
for f in fis:
fname = os.path.splitext(f) # Split a binary group of file names and extensions
new = fname[0] + 'b' + fname[1] # Change a name
os.rename(os.path.join(dire,f),os.path.join(dire,new)) # rename
os.path.walk(root,func,()) # traverse
The problem with this method is that it doesn't recursively iterate through the next level.
The advanced version of python includes os.walk(), which is better than this.
OS. Walk ()
Prototype: os.walk(top, topdown=True, onerror=None, followlinks=False)
We usually only use the first parameter. (topdown indicates the order of traversal)
This method returns a triple for each directory (dirpath, dirnames, filenames). The first is the path, the second is the directory under the path, and the third is the non-directory under the path (in the case of Windows, the file). See an example:
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 way you can recursively iterate through all the files.
listdir
Several methods under the OS module can be combined for traversal. See an example:
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
The important thing to note here is that the I is the directory or file name, not the full path. When used, restore the full path with the os.path.join() method.
After traversing, file name changes can be done with some advanced processing using regular expressions.
In addition, you can use os.system(CMD) to invoke the relevant commands in the shell to process files, which is very nice and powerful.