# -*- coding: utf-8 -*-
"""
Created on Wed Nov 14 10:12:47 2018
@author: Administrator
"""
import os
#import pandas as pd
#flagpath='F:/公司项目/2018年项目/中加app/数据/python导出excel数据表/健康风险数据/风险分析/2018年6月至8月PM2.5风险分析' #文件夹所在的路径
flagpath='F:/公司项目/2018年项目/中加app/数据/python导出excel数据表/健康风险数据/疾病分析/01 呼吸系统住院率 2017.12至2018.1' #文件夹所在的路径
#countrypath='G:/touchpal/code/nation_flag/country_ code.csv' #国家简称与国家代码对应表格路径
filelist=os.listdir(flagpath) #获取所有文件名
#country_code=pd.read_csv(countrypath) #读取表格
for files in filelist:
olddir=os.path.join(flagpath,files) #修改前文件路径及文件名
filename=os.path.splitext(files)[0] #得到文件名
filetype=os.path.splitext(files)[1] #得到文件类型
newfilename=filename #先用旧的文件名覆盖新文件名,防止没有新文件名
# m=shape(country_code)[0] #获取国家简称与国家代码对应表格(DataFrame)行数
# for i in range(m):
# if country_code['Alpha_code'][i]==filename: #匹配与转化
# newfilename=country_code['Numeric code'][i] #匹配成功复制新文件名
newname=str(newfilename)[0:19]+filetype #连接文件名与文件类型
print(newname)
newdir=os.path.join(flagpath,newname) #新的文件路径及文件名
os.rename(olddir,newdir) #重命名,覆盖原先的文件名
https://blog.youkuaiyun.com/moxigandashu/article/details/77450130