Do you want to rename a large number of text files and image files at once? This time, I will explain such "how to change the file name at once".
Prepare the file to be renamed. The file can be a text file, an image file, a music file, or a video file. Prepare your favorite file. ↓ ↓ We are preparing an image file this time ↓ ↓ : warning: Attention: warning: Be sure to back up your data so that you can make mistakes! !!
Use the "os.rename ()" function to rename the file. You can rename it by specifying the file before the change in the first argument and the file after the change in the second argument.
python
import os
os.rename(File before change,File after change)
python
# coding: utf-8
import glob
import os
#extension.Get png image file
path = './dir/*.png'
i = 1
#Get an image file
before_file_list = glob.glob(path)
print('Change before')
print(before_file_list)
#Change file names at once
for file in before_file_list:
os.rename(file, './dir/icon' + str(i) + '.png')
i += 1
after_file_ist = glob.glob(path)
print('After change')
print(after_file_ist)
↓ ↓ Image file after renaming ↓ ↓ This sample source is also posted on Github. Github:https://github.com/miyazakikna/RenameFile.git
Here, I explained how to rename files all at once using Python. It is very useful personally because it is simple and you can easily change a large number of file names at once.
Recommended Posts