美文网首页
python提取文件夹内指定类型文件

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

作者: 逍遥才子 | 来源:发表于2019-06-11 10:05 被阅读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)
    
    
    
    
    

    相关文章

      网友评论

          本文标题:python提取文件夹内指定类型文件

          本文链接:https://www.haomeiwen.com/subject/zbrffctx.html