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 open()文件处理使用介绍
Nov 30 Python
python中while循环语句用法简单实例
May 07 Python
Python类的定义、继承及类对象使用方法简明教程
May 08 Python
Python实现列表转换成字典数据结构的方法
Mar 11 Python
python中不能连接超时的问题及解决方法
Jun 10 Python
十行代码使用Python写一个USB病毒
Jun 21 Python
python多线程共享变量的使用和效率方法
Jul 16 Python
详解python中的生成器、迭代器、闭包、装饰器
Aug 22 Python
python针对mysql数据库的连接、查询、更新、删除操作示例
Sep 11 Python
django自定义模板标签过程解析
Dec 14 Python
Python小整数对象池和字符串intern实例解析
Mar 21 Python
python Tornado框架的使用示例
Oct 19 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制作图型计数器的例子
2006/10/09 PHP
Godaddy空间Zend Optimizer升级方法
2010/05/10 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
2013/06/18 PHP
php实现模拟登陆方正教务系统抓取课表
2015/05/19 PHP
php实现将Session写入数据库
2015/07/26 PHP
css图片自适应大小
2007/11/28 Javascript
jquery实现的鼠标下拉滚动置顶效果
2014/07/24 Javascript
浅谈轻量级js模板引擎simplite
2015/02/13 Javascript
JQuery中attr方法和removeAttr方法用法实例
2015/05/18 Javascript
vue中渐进过渡效果实现
2016/10/27 Javascript
Require.js的基本用法详解
2017/07/03 Javascript
jquery ajax异步提交表单数据的方法
2017/10/27 jQuery
JS实现验证码倒计时的注册页面
2018/01/02 Javascript
JS实现移动端双指缩放和旋转方法
2019/12/13 Javascript
OpenLayers3实现测量功能
2020/09/25 Javascript
如何在vue中使用kindeditor富文本编辑器
2020/12/19 Vue.js
使用Python脚本和ADB命令实现卸载App
2017/02/10 Python
Django使用httpresponse返回用户头像实例代码
2018/01/26 Python
Django自定义用户认证示例详解
2018/03/14 Python
Python 导入文件过程图解
2019/10/15 Python
鲜为人知的HTML5语音合成功能
2019/05/17 HTML / CSS
定制iPhone和Macbook保护壳:Slick Case
2018/11/21 全球购物
丝芙兰新加坡官网:Sephora新加坡
2018/12/04 全球购物
恶意软件的定义
2014/11/12 面试题
2015年元旦活动总结
2014/05/09 职场文书
刑事辩护授权委托书
2014/09/13 职场文书
高中生毕业评语
2014/12/30 职场文书
就业推荐表自我评价范文
2015/03/02 职场文书
辛亥革命观后感
2015/06/02 职场文书
党支部意见范文
2015/06/02 职场文书
幼儿园小班教师随笔
2015/08/14 职场文书
用几道面试题来看JavaScript执行机制
2021/04/30 Javascript
教你怎么用python爬取爱奇艺热门电影
2021/05/20 Python
springboot 启动如何排除某些bean的注入
2021/08/02 Java/Android
Nginx源码编译安装过程记录
2021/11/17 Servers
详解Python中的for循环
2022/04/30 Python