python3 批量修改文件扩展名——递归

 2023-09-05 阅读 474 评论 0

摘要:python3 批量修改文件扩展名——递归 ''' Created on 2017年9月28日@author: kearney '''import osdef isFile(filePath):#修改文件扩展名filename=filePath.split('\\')[-1]#拆分文件路径获得文件名fatherPath=filePath.repla
python3 批量修改文件扩展名——递归

 

'''
Created on 2017年9月28日@author: kearney
'''import osdef isFile(filePath):#修改文件扩展名filename=filePath.split('\\')[-1]#拆分文件路径获得文件名fatherPath=filePath.replace(filename,'')#获得父级路径split=os.path.splitext(filename)#拆分文件名和扩展名newname=split[0]+'.xls'#生成新文件名os.chdir(fatherPath)#改变当前工作目录到指定的路径os.rename(filename,newname)#文件重命名def openDir(filePath):#递归文件夹pathDir=os.listdir(filePath)#返回包含的文件或文件夹的名字的列表for filename in pathDir:#遍历列表childPath=os.path.join(filePath,filename)#判断是否为文件夹if os.path.isfile(childPath):isFile(childPath)else:openDir(childPath)rootDir=r'D:\\files\文件夹'#根目录
pathDir =  os.listdir(rootDir)#列出根目录下所有内容for allDir in pathDir:#遍历列表filepath=os.path.join(rootDir,allDir)#文件路径合成#判断是否为文件夹if os.path.isfile(filepath):isFile(filepath)else:openDir(filepath)

 

posted on 2017-09-29 16:12 Gino森 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/kearney908/p/7611520.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/407.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息