博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python文件夹复制
阅读量:6887 次
发布时间:2019-06-27

本文共 2463 字,大约阅读时间需要 8 分钟。

hot3.png

import os,sys maxfileload=1000000 blksize=1024*500

def copyfile(pathFrom,pathTo,maxfileload=maxfileload): """ :param pathFrom: :param pathTo: :param maxfileload: :return:

将单个文件逐字节从pathFrom复制到pathTo; 使用二进制文件模式阻止Unicode解码和换行"""if os.path.getsize(pathFrom)<=maxfileload:    bytesFrom=open(pathFrom,'rb').read()    open(pathTo,'wb').write(bytesFrom)else:    fileFrom=open(pathFrom,'rb')    fileTo=open(pathTo,'wb')    while True:        bytesFrom=fileFrom.read(blksize)        if not bytesFrom:break        fileTo.write(bytesFrom)

def copytree(dirFrom,dirTo,verbose=0): """ 将敌人From下的内容复制到dirTo,返回(文件和目录)数目形式的元组 为避免在某些平台上目录名不可解码,可能需要为其名使用字节 :param dirFrom: :param dirTo: :param verbose: :return: """

fcount=dcount=0for 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('Error creating',pathTo,'--skipped')            print(sys.exc_info()[0],sys.exc_info()[1])    else:        if verbose:print('copying dir',pathFrom,'To',pathTo)        try:            os.mkdir(pathTo)            below=copytree(pathFrom,pathTo)            fcount+=below[0]            dcount+=below[1]            dcount +=1        except:            print('Error creating', pathTo, '--skipped')            print(sys.exc_info()[0], sys.exc_info()[1])return (fcount,dcount)

def getArgs(): """ 获取验证文件目录名参数 :return: """

try:    dirFrom,dirTo=sys.argv[1:]except:    print('Usage error:cpall.py dirFrom dirTo')else:    if not os.path.isdir(dirFrom):        print('ErrorLdirFrom is not a directory')    elif not os.path.exists(dirTo):        os.mkdir(dirTo)        print('Note:dirTo was created')        return (dirFrom,dirTo)    else:        print('Waring:dirTo already exist.')        if hasattr(os.path,'samefile'):            same=os.path.samefile(dirFrom,dirTo)        else:            same=os.path.abspath(dirFrom)==os.path.abspath(dirTo)        if same:            print('Error:dirFrom same as dirto')        else:            return (dirFrom,dirTo)

if name=='main': import time distuple=getArgs() if distuple: print('Copying...') start=time.clock() fcount,dcount=copytree(*distuple) print('Copied',fcount,'files',dcount,'directions',end=' ') print('in ',time.clock()-start,'seconds')

源代码在:

转载于:https://my.oschina.net/puzhiyuan/blog/851959

你可能感兴趣的文章
深度学习论文阅读路线图
查看>>
《智能家居产品 从设计到运营》——2.3 智能设备互联的语言:通信协议
查看>>
如何自己注册域名?什么样的域名是好域名?
查看>>
除了 Markdown 编辑器,你还需要会用程序来处理它
查看>>
Linux有问必答:如何在Linux中修改环境变量PATH
查看>>
【ATF】林伟:大数据计算平台的研究与实践
查看>>
STL--双端队列(deque)和链表(list)
查看>>
x264代码剖析(十二):核心算法之帧内预测函数x264_mb_analyse_intra()
查看>>
c++ 命名空间 以及 作用域 函数参数 面向对象实验报告
查看>>
【RAC】在所有节点上滚动安装BUNDLE Patch for Base Bug 9413827补丁包
查看>>
Apache Storm 官方文档 —— Metrics
查看>>
百度地图 demo 在html中显示 在jsp中不显示
查看>>
Mac下安装Caffe
查看>>
RDS-MSSQL问题排查方法
查看>>
实现u-boot对yaffs/yaffs2文件系统下载的支持
查看>>
[算法系列之十三]Rabin-Karp字符串查找算法
查看>>
智能合约从入门到精通:完整范例
查看>>
将CLA引入开源贡献
查看>>
微信小程序--倒计时封装
查看>>
区块链为品牌企业和用户解决“信任”危机
查看>>