Python bulk renaming of files in the same folder

Source: Internet
Author: User
Tags python script

The example in this article describes how Python bulk renames files in the same folder. Share to everyone for your reference. The specific analysis is as follows:

A friend sent a folder over, and the pictures in it were suffix. tmp.

Manual modification is too much work. So write a Python script for batch renaming.

Not familiar with Python's standard library, you can only check the data, or help () side write code.

Three lines of code can solve this problem.

However, no exceptions are caught and all files in the same directory cannot be iterated.

The code is as follows:

?

1 2 3 4 Import os for file in Os.listdir ("."): If Os.path.splitext (file) [1] = = ". tmp": os.rename (file, os.path.splitext (file) [0]+ ". jpg"

Save the code to a file and place it in the folder where you want to rename the file, and run it.

The ability of Python to accomplish this small demand is really nothing to say.

I hope this article will help you with your Python programming.

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.