批量修改文件名
原始文件中含有中文信息+英文信息,希望生成另一種標準格式但是要包含中文姓名。
舉個例子就是,下圖是原始文件名:
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
最後文件重命名之後;
總之,工作使人成長吧,哈哈哈....