How to batch rename files in the same folder in Python

Source: Internet
Author: User

How to batch rename files in the same folder in Python

This example describes how to batch rename files in the same folder in Python. Share it with you for your reference. The specific analysis is as follows:

A friend sent a folder and all the images in it were suffixed with. tmp.

Manual modification requires too much work. Therefore, a Python script is written for batch renaming.

Not familiar with the Python standard library, you can only query the information, or help () while writing 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. Run the command.

There is nothing to say about Python's ability to meet this small requirement.

I hope this article will help you with 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.