python批量修改文件名

# -*- 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.csdn.net/moxigandashu/article/details/77450130

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值