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读取html中指定元素生成excle文件示例
Apr 03 Python
Python判断变量是否已经定义的方法
Aug 18 Python
用Python编写一个简单的FUSE文件系统的教程
Apr 02 Python
Python减少循环层次和缩进的技巧分析
Mar 15 Python
Python使用urllib2模块抓取HTML页面资源的实例分享
May 03 Python
Pycharm使用之设置代码字体大小和颜色主题的教程
Jul 12 Python
Python paramiko模块使用解析(实现ssh)
Aug 30 Python
Python+Selenium+phantomjs实现网页模拟登录和截图功能(windows环境)
Dec 11 Python
python几种常用功能实现代码实例
Dec 25 Python
python爬虫开发之urllib模块详细使用方法与实例全解
Mar 09 Python
利用Vscode进行Python开发环境配置的步骤
Jun 22 Python
 Python 中 logging 模块使用详情
Mar 03 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
PHP 防注入函数(格式化数据)
2011/08/08 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
javascript获取当前ip的代码
2009/05/10 Javascript
asp.net网站开发中用jquery实现滚动浏览器滚动条加载数据(类似于腾讯微博)
2012/03/14 Javascript
基于javascript 闭包基础分享
2013/07/10 Javascript
表单元素与非表单元素刷新区别详细解析
2013/11/06 Javascript
JQuery结合CSS操作打印样式的方法
2013/12/24 Javascript
使用javascript实现雪花飘落的效果
2015/01/13 Javascript
JS数组合并push与concat区别分析
2015/12/17 Javascript
AngularJS实现Input格式化的方法
2016/11/07 Javascript
bootstrap css样式之表单
2017/01/19 Javascript
微信小程序tabBar底部导航中文注解api详解
2017/08/16 Javascript
详解vue 计算属性与方法跟侦听器区别(面试考点)
2018/04/23 Javascript
介绍Python的Django框架中的静态资源管理器django-pipeline
2015/04/25 Python
python实现识别相似图片小结
2016/02/22 Python
简单谈谈Python流程控制语句
2016/12/04 Python
Python进度条实时显示处理进度的示例代码
2018/01/30 Python
python实现得到当前登录用户信息的方法
2019/06/21 Python
Python Django 命名空间模式的实现
2019/08/09 Python
Python实现名片管理系统
2020/02/14 Python
Python利用myqr库创建自己的二维码
2020/11/24 Python
详解HTML5如何使用可选样式表为网站或应用添加黑暗模式
2020/04/07 HTML / CSS
AmazeUI 网格的实现示例
2020/08/13 HTML / CSS
来自世界各地的饮料:Flavourly
2019/05/06 全球购物
DOUGLAS荷兰:购买香水和化妆品
2020/10/24 全球购物
高考自主招生自荐信
2013/10/20 职场文书
《木笛》教学反思
2014/03/01 职场文书
个人社会实践自我鉴定
2014/03/24 职场文书
工程承包协议书
2014/04/22 职场文书
学习经验演讲稿
2014/05/10 职场文书
地球一小时宣传标语
2014/06/24 职场文书
搞笑婚庆主持词
2015/06/29 职场文书
教师素质教育心得体会
2016/01/19 职场文书
毕业季聚会祝酒词!
2019/07/04 职场文书
python实现网络五子棋
2021/04/11 Python
Python OpenCV超详细讲解读取图像视频和网络摄像头
2022/04/02 Python