python2.7实现复制大量文件及文件夹资料


Posted in Python onAugust 31, 2019

需求:拷大量数据,发现有2000G,靠系统的复制功能怕是得好几个小时,于是回来学一手操作,话不多说上代码:

说明: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)
 
#实战代码
#!/usr/bin/python2
# coding=UTF-8
#@author neo_will
#version 2019-04-02 10:39
 
import os
import os.path
import shutil
import time, datetime
 
#fpath_2018 = [1207, 1121, 1120, 1119, 1112, 1101, 1025, 1009, 0704, 0608, 0531, 0530, 0517, 0502, 0418, 0330, 0201, 0131]
#sourceDir=r"F:\LEVEL2_shanghai\2018\fpath_2018[0:]"
#des_dir=r"G:\MarketDataSupplement\shanghai\2018\fpath_2018[0:]"
#原始目录和拷贝到的目录地址
sourceDir = r"D:\tools\wj"
targetDir = r"D:\Users\wj"
copyFileCounts = 0
 
#定义拷贝文件的函数
def copyFiles(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 u"%s %s copy over" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)
 else:
  print("%s %s is exists,please don't copy more" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF))
 
 if os.path.isdir(sourceF):
 copyFiles(sourceF, targetF)
 
if __name__ == "__main__":
 time_start = time.time()
 try:
 import psyco
 psyco.profile()
 except ImportError:
 pass
 #copyFiles(sourceDir,targetDir)
 copyFiles(r"D:\tools\wj",r"D:\Users\wj")
 time_end = time.time()
 print('totally cost', time_end - time_start)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
浅谈python中scipy.misc.logsumexp函数的运用场景
Jun 23 Python
在python win系统下 打开TXT文件的实例
Apr 29 Python
Python读写docx文件的方法
May 08 Python
mac 安装python网络请求包requests方法
Jun 13 Python
使用python opencv对目录下图片进行去重的方法
Jan 12 Python
Python使用itchat 功能分析微信好友性别和位置
Aug 05 Python
Django框架 Pagination分页实现代码实例
Sep 04 Python
python集成开发环境配置(pycharm)
Feb 14 Python
python GUI库图形界面开发之PyQt5简单绘图板实例与代码分析
Mar 08 Python
浅谈keras中的keras.utils.to_categorical用法
Jul 02 Python
Python 开发工具通过 agent 代理使用的方法
Sep 27 Python
python3.9.1环境安装的方法(图文)
Feb 02 Python
python3实现高效的端口扫描
Aug 31 #Python
python nmap实现端口扫描器教程
May 28 #Python
Python3多线程版TCP端口扫描器
Aug 31 #Python
简单了解python协程的相关知识
Aug 31 #Python
利用rest framework搭建Django API过程解析
Aug 31 #Python
Python进度条的制作代码实例
Aug 31 #Python
python类的实例化问题解决
Aug 31 #Python
You might like
仿AS3实现PHP 事件机制实现代码
2011/01/27 PHP
Yii2 加载css、js 载静态资源的方法
2017/03/10 PHP
JQuery UI DatePicker中z-index默认为1的解决办法
2010/09/28 Javascript
基于JavaScript实现继承机制之调用call()与apply()的方法详解
2013/05/07 Javascript
多种方法判断Javascript对象是否存在
2013/09/22 Javascript
让jQuery Mobile不显示讨厌loading界面的方法
2014/02/19 Javascript
javascript初学者常用技巧
2014/09/02 Javascript
基于jQuery实现的美观星级评论打分组件代码
2015/10/30 Javascript
jQuery实现文字自动横移
2017/01/08 Javascript
php main 与 iframe 相互通讯类(js+php同域/跨域)
2017/09/14 Javascript
JQuery Ajax动态加载Table数据的实例讲解
2018/08/09 jQuery
js实现搜索栏效果
2018/11/16 Javascript
详解服务端预渲染之Nuxt(介绍篇)
2019/04/07 Javascript
Vue数据双向绑定原理实例解析
2020/05/15 Javascript
vue 基于abstract 路由模式 实现页面内嵌的示例代码
2020/12/14 Vue.js
[01:34]DOTA2 7.22版本新增神杖效果一览(敏捷英雄篇)
2019/05/28 DOTA
python爬虫的工作原理
2017/03/05 Python
python 换位密码算法的实例详解
2017/07/19 Python
Python实现PS图像调整之对比度调整功能示例
2018/01/26 Python
对Python中实现两个数的值交换的集中方法详解
2019/01/11 Python
Python @property使用方法解析
2019/09/17 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
2020/04/02 Python
在Keras中实现保存和加载权重及模型结构
2020/06/15 Python
CSS3字体效果的设置方法小结
2016/06/13 HTML / CSS
CSS3样式linear-gradient的使用实例
2017/01/16 HTML / CSS
Vans奥地利官方网站:美国原创极限运动潮牌
2018/09/30 全球购物
小学生成长感言
2014/01/30 职场文书
爱情保证书范文
2014/02/01 职场文书
搞笑爱情保证书
2014/04/29 职场文书
感恩老师的演讲稿
2014/05/06 职场文书
新文化运动的口号
2014/06/21 职场文书
建筑安全生产目标责任书
2014/07/23 职场文书
银行奉献演讲稿
2014/09/16 职场文书
Nginx工作原理和优化总结。
2021/04/02 Servers
解决vue $http的get和post请求跨域问题
2021/06/07 Vue.js
图文详解matlab原始处理图像几何变换
2021/07/09 Python