Python3 realizes batch modification of file names for your reference. The specific content is as follows
Take the batch modification of all the picture names in a folder as an example. The annotations are super detailed and the universal template. Readers can draw inferences from one another. Modify the template appropriately, and the effect is remarkable!
# Batch modify file name
# Modify image file names in batch
import os
import re
import sys
def renameall():
fileList = os.listdir(r"C:\Users\Administrator\Desktop\stars") #To be modified folder
print("before fixing:"+str(fileList)) #Files contained in the output folder
currentpath = os.getcwd() #Get the current working directory of the process
os.chdir(r"C:\Users\Administrator\Desktop\stars") #Modify the current working directory to the location of the folder to be modified
num=1 #Name variable
for fileName in fileList: #Traverse all files in the folder
pat=".+\.(jpg|png|gif)" #Regular expression for matching file names
pattern = re.findall(pat,fileName) #Make a match
os.rename(fileName,(str(num)+'.'+pattern[0])) #File rename
num = num+1 #Change the number and continue to the next item
print("---------------------------------------------------")
os.chdir(currentpath) #Change back to the working directory before the program was run
sys.stdin.flush() #Refresh
print("After modification:"+str(os.listdir(r"C:\Users\Administrator\Desktop\stars"))) #Output the files contained in the modified folder
renameall()
Run the program:
After modification, the display in the folder:
The modification is successful and we are very satisfied!
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts