kearney

自学还没成才的python猿

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

'''
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森  阅读(931)  评论(0编辑  收藏  举报