import os
def change_file_name(path,old_str,new_str):
fileList = os.listdir(path)
for file in fileList:
#如果你的目标文件夹也是docx类型的,这里不用修改,其他的根据情况修改
if not file.endswith('.docx'):
fileList.remove(file)
for i in fileList:
# 设置旧文件名(就是路径+文件名)
oldname = path + os.sep + i # os.sep添加系统分隔符
new = i.replace(old_str, new_str)
# 设置新文件名,加号后面的数字是起步位置
newname = path + os.sep + new
os.rename(oldname, newname) # 用os模块中的rename方法对文件改名
print(oldname, '======>', newname)
# 获取该目录下所有文件,存入列表中
path = 'E:\\案件相关\\大理\\数据处理\\数据测试'
old_str=input('请输入之前的字符串:')
new_str=input('请输入之后的字符串:')
change_file_name(path,old_str,new_str)
批量修改目录下的文件名
最新推荐文章于 2024-03-04 16:13:53 发布