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森 阅读(...) 评论(...) 编辑 收藏