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

 

'''
Created on 2017年9月28日

@author: kearney
'''

import os

def 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值