批量修改文件名
原始文件中含有中文信息+英文信息,希望生成另一种标准格式但是要包含中文姓名。
举个例子就是,下图是原始文件名:
code如下:
###rename files
import os
import re
pattern=re.compile(u'[\u4e00-\u9fa5]+')
#正则表达式匹配中文
path='C:\\Users\\wangyana\\Documents\\new\\'#文件存储位置
fileAllName=os.listdir(path)#获取文件位置
fileAllName #查看文件名
i=0
for name in fileAllName:#遍历所有的文件名字
fileformat=fileAllName[i][fileAllName[i].find("."):] #文件格式
doctorName = re.findall(pattern,fileAllName[i]) #提取人名
doctorName ="".join(doctorName ) #转换数据格式
doctorName =doctorName.strip() #去除空格
newName = "KOL_Network_"+doctorName+fileformat #连接字段
print(newName) #这步只是我用来查看结果的
os.rename(path+fileAllName[i],path+newName)
i=i+1
最后文件重命名之后;
总之,工作使人成长吧,哈哈哈....