python批量修改图片名
程序员文章站
2022-04-18 19:54:03
...
修改文件名为“1.jpg”、“2.jpg”……
import os
import re
"""批量修改文件夹的图片名"""
def ReFileName(dirPath,pattern):
"""
:param dirPath: 文件夹路径
:pattern:正则
:return:
"""
# 对目录下的文件进行遍历
i = 1
for filename in os.listdir(dirPath):
newname = str(i) + ".jpg"
newFilename = filename.replace(filename, newname)
os.rename(os.path.join(dirPath, filename), os.path.join(dirPath, newFilename))
i += 1
print("图片名已全部修改成功")
if __name__ == '__main__':
dirPath = r"D:\Word\deeplearning\农作物病虫害识别\1\ai_challenger_pdr2018_testb_20181023\AgriculturalDisease_testB\images"
pattern = re.compile(r'.*')
ReFileName(dirPath,pattern)