Python batch renames the pictures in the file

Python batch renames the pictures in the file

Some are shown below 内联代码片.

import os
import re
 
def ReName(dir_Path,pattern):
 
    i = 1#从1开始命名
    for filename in os.listdir(dir_Path): #获取文件夹内所有文件的名字
        print(filename)
        new_filename = 't_' + str(i) + ".jpg"#定义新的文件名
        print(new_filename)#打印新的文件名字
        os.rename(os.path.join(dir_Path, filename), os.path.join(dir_Path, new_filename))#用rename()函数重命
        i = i + 1
 
    print("----------Success!-------------")
 
if __name__ == '__main__':
 
    dir_Path = r"E:\Apple\test\image"
    pattern = re.compile(r'.*')
    ReName(dir_Path,pattern)

result:
insert image description here

Guess you like

Origin blog.csdn.net/AKxiaokui/article/details/129092580