python之对指定目录文件夹的批量重命名

时间:2023-06-14 08:36:08

python之对指定目录文件夹的批量重命名

import os,shutil,string
dir = "/Users/lee0oo0/Documents/python/test" #指定的目录
for i in os.listdir(dir): #遍历指定目录的文件
newfile = i.replace('a','b') # 替换
oldname = dir +'/'+str(i)
newname = dir +'/'+str(newfile)
shutil.move(oldname,newname) # 重命名
print("finish")