python提取文件夹内指定类型文件

2019-06-11  本文已影响0人  逍遥才子

import os,shutil
import re
path = 'G:\琴趣科技乐谱\小组完成乐谱的副本'
targetDir = 'G:\输出'


def gci (path):
    """this is a statement"""
    parents = os.listdir(path)
    for parent in parents:
        child = os.path.join(path,parent)
        if os.path.isdir(child):
            gci(child)
        # print(child)
        else:
            if os.path.splitext(child)[1] == '.sib':
                fpath,fname=os.path.split(child)
                filename, extension = os.path.splitext(fname)
                str = re.sub("[0-9\+\\\!\%\[\]\,\。]", "", filename)
                mycopyfile(child,targetDir+"/"+str+".sib")
                print(str)



def mycopyfile(srcfile,dstfile):
    if not os.path.isfile(srcfile):
        print('不是文件')
    else:
        fpath,fname=os.path.split(dstfile)    #分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)                #创建路径
        shutil.copyfile(srcfile,dstfile)      #复制文件
        print('复制完成')


gci(path)




上一篇下一篇

猜你喜欢

热点阅读