工作中经常会遇到需要批量修改文件名的情况,比如:
有这样一个文件夹,里面是249个国家的国家,按照ISO 3166-1标准中的国家二位简称进行命名:
现在面临这样的需求,需要将二位英文简称命名的文件换成国家代码命名的文件。
比如,第一个国家Andorra,简称AD,根据表格对应的国家码,需要将AD.png修改为20.png。
资源下载链接:http://download.csdn.net/download/moxigandashu/9943276
接下来,我们利用Python实现以上要求:
import os
import pandas as pd
flagpath='G:/touchpal/code/nation_flag/xhdpi'
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]
for i in range(m):
if country_code['Alpha_code'][i]==filename:
newfilename=country_code['Numeric code'][i]
newname=str(newfilename)+filetype
newdir=os.path.join(flagpath,newname)
os.rename(olddir,newdir)
这个时候再看刚刚各国国家国旗所在的文件夹:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)