美文网首页
python 复制文件

python 复制文件

作者: 喵呜e喵星人 | 来源:发表于2020-08-30 14:34 被阅读0次
    import os,sys
    maxfileload =10000000
    blksize = 1024*500
    
    def copyfile(pathFrom,pathTo,maxfileload=maxfileload):
        '''
        将单个文件逐字节从pathFrom复制到pathTo;
        使用二进制文件模式阻止Unicode解码及换行符转换。
        '''
        if os.path.getsize(pathFrom) <= maxfileload:
            bytesFrom = open(pathFrom,'rb').read()
            open(pathTo,'wb').write(bytesFrom)
        else:
            fileForm = open(pathFrom,'rb')
            fileTo = open(pathTo,'wb')
            while True:
                bytesFrom = fileForm.read(blksize)
                if not bytesFrom:break
                fileTo.write(bytesFrom)
    
    
    def copyTree(dirFrom,dirTo,verbose=0):
        '''
        将dirFrom下的内容复制到dirTo,返回文件目录,数目形式的元祖;
        为避免在某些平台上目录名不可解码,可能需要为其名使用字节;
        在Unix下可能需要更多文件类型检查:跳过链接,fifo之类。
        '''
        fcount = dcount = 0
        for filename in os.listdir(dirFrom):
            pathFrom = os.path.join(dirFrom,filename)
            pathTo = os.path.join(dirTo,filename)
            if not os.path.isdir(pathFrom):
                try:
                    if verbose>=1:print('copying',pathFrom,'to',pathTo)
                    copyfile(pathFrom,pathTo)
                    fcount += 1
                except:
                    print("复制出错,从",pathFrom,'到',pathTo )
                    print(sys.exc_info()[0],sys.exc_info()[1])
            else:
                if verbose: print('复制目录',pathFrom,'到',pathTo)
                try:
                    os.mkdir(pathTo)
                    below = copyTree(pathFrom,pathTo)
                    fcount +=below[0]
                    dcount +=below[1]
                    dcount +=1
                except:
                    print("复制出错",pathTo,'---skipped')
                    print(sys.exc_info()[0],sys.exc_info()[1])
        return (fcount,dcount)
    
    def getargs():
        '''
        获取并验证文件目录名参数,碰到错误是默认返回None
        '''
        try:
            dirForm,dirTo = sys.argv[1:]
        except:
            print("参数错误,使用cpall.py 源文件夹名 目标文件夹名")
        else:
            if not os.path.isdir(dirForm):
                print("错误:源文件夹不存在!")
            elif not os.path.exists(dirTo):
                os.mkdir(dirTo)
                print("注意:创建了目标文件夹.")
                return (dirForm,dirTo)
            else:
                print("警告:目标文件夹已存在")
                if hasattr(os.path,'samefile'):
                    same = os.path.samefile(dirForm,dirTo)
                else:
                    same = os.path.abspath(dirForm) == os.path.abspath(dirTo)
                if same:
                    print("出错:目标文件和源文件一致")
                else:
                    return (dirForm,dirTo)
    
    if __name__ =="__main__":
        import time
        dirstuple = getargs()
        if dirstuple:
            print("开始复制........")
            start = time.clock()
            fcount,dcount = copyTree(*dirstuple)
            print("copied",fcount,'files',dcount,'directories',end='')
            print('in',time.clock()-start,'seconds')
    

    相关文章

      网友评论

          本文标题:python 复制文件

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