1导入包
import os
import re
2定义修改文件函数
def ReFileName(dirPath,pattern):
# 对目录下的文件进行遍历
i = 1
for file in os.listdir(dirPath):
# 判断是否是文件
if os.path.isfile(os.path.join(dirPath, file)) == True:
#c= os.path.basename(file)
newName = re.sub(pattern, '猫'+str(i)+'.jpg', file)
# 修改成你需要的名字
newFilename = file.replace(file, newName)
# 重命名
os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newFilename))
i+=1
print("图片名已全部修改成功")
3.定义主函数
if __name__ == '__main__':
dirPath = r"" #你需要修改文件位置,可以是绝对路径也可以是相对路径
pattern = re.compile(r'.*')
ReFileName(dirPath,pattern)
这个很简单的,会Python基本上就可以懂