3 Methods for traversing objects in python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.