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.