python实现复制大量文件功能
2019-08-31 11:50
751 查看
本文实例为大家分享了python实现复制大量文件的具体代码,供大家参考,具体内容如下
本来是去项目公司拷数据,结果去了发现有500G,靠系统的复制功能怕是得好几个小时,于是回来学一手操作,话不多说上代码:
说明:CopyFiles1是可以将sourceDir连子目录一起原样复制到targetDir,而CopyFiles2是在sourceDir中筛选特定格式文件,然后将其直接放在targetDir中,会很乱,但是很快
import os import time import shutil sourceDir = r"D:\copytest\datatest" targetDir = r"D:\copytest\result" copyFileCounts = 0 def CopyFiles1(sourceDir, targetDir): #完全连子目录也会复制好,美观 global copyFileCounts print(sourceDir ) print("%s 当前处理文件夹%s已处理%s 个文件" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), sourceDir,copyFileCounts) ) for f in os.listdir(sourceDir): sourceF = os.path.join(sourceDir, f) targetF = os.path.join(targetDir, f) if os.path.isfile(sourceF): if not os.path.exists(targetDir): os.makedirs(targetDir) copyFileCounts += 1 if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))): open(targetF, "wb").write(open(sourceF, "rb").read()) print ("%s %s 复制完毕" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)) else: print ("%s %s 已存在,不重复复制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)) if os.path.isdir(sourceF): copyFiles(sourceF, targetF) def CopyFiles2(dir): #会将目录下所有文件都复制在一起,速度快,可以筛选文件 i=0 for root,dir1,filename in os.walk(dir): #print(filename) for index in range(len(filename)): #print(os.path.splitext(filename[index])[1]) #if os.path.splitext(filename[index])[1]=='.':#这里注意filename是个元组,splitext方法的时候只能是字符串 if 1==1: #i+=1 print('here') root1="D:\\copytest\\result3" old_path = os.path.join(root, filename[index]) print(old_path) new_path = os.path.join(root1,filename[index]) shutil.copyfile(old_path,new_path) #print("总共有",i,"图层文件被复制!") if __name__ == "__main__": time_start = time.time() try: import psyco psyco.profile() except ImportError: pass #CopyFiles1(sourceDir,targetDir) CopyFiles2("D:/copytest/datatest") time_end = time.time() print('totally cost', time_end - time_start)
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- python2.7实现复制大量文件及文件夹资料
- 【Python/PE】python中实现文件操作的几种功能(删除指定目录下的所有内容,文件复制等)
- 用python 实现上传文件功能
- Python 实现文件复制、删除
- java实现文件复制功能
- D-Bus入门(四)——QTDBUS代码,实现ofono代码下的python测试文件activite-context的功能
- Python 实现文件复制、删除
- python脚本实现文件的批量移动、复制,以及删除指定后缀
- Python实现的将文件每一列写入列表功能示例【测试可用】
- js实现复制功能(文件上传)
- Python 实现一个小功能: 删除某路径下文件及文件夹的脚本
- Android手机SD卡文件或目录拷贝、复制、粘贴功能实现代码
- 【Python】 2种实现文件复制的方法
- python实现上传下载文件功能
- 实现文件复制的功能(从原路径下 复制 图片 到 新路径下 )
- C语言实现文件复制功能
- python3-文件的修改——实现类似shell中sed的功能
- 使用文件操作函数实现:文件的复制功能。
- Python os.path和shutil模块实现文件复制、删除
- python复制文件代码实现