python如批量何修改文件名称(python修改文件名称的命令)
这操作很骚,必须会用。
1.目的:
想把files文件夹下的excel文件改掉,这里假设改为’fileA.xlsx’,’fileB.xlsx’,如何使用python自动更改。
file1.xlsx,file.xlsx改为fileA.xlsx,fileB.xlsx
2.修改手段:
利用os.rename模块,以及前面讲的glob模块。
首先获取文件夹下的excel文件:
import glob,oschangeFileType = '*.xlsx'path = 'files'def getFileLst(path,changeFileType): fileLst = glob.glob(os.path.join(path,changeFileType)) return fileLstfileList = getFileLst(path,changeFileType)
输出fileList:[‘files\file1.xlsx’, ‘files\file2.xlsx’]
修改代码:
newNameLst = ['fileA.xlsx','fileB.xlsx']for oldName,newName in zip(fileList,newNameLst): newName = os.path.join(path,newName) os.rename(oldName, newName)
效果:
修改后的结果
该操作可用于批量修改文件名称。
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com