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函数参数的区别(必看篇)
May 29 Python
浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
Apr 10 Python
多个应用共存的Django配置方法
May 30 Python
Python2.7环境Flask框架安装简明教程【已测试】
Jul 13 Python
Django框架自定义模型管理器与元选项用法分析
Jul 22 Python
基于django ManyToMany 使用的注意事项详解
Aug 09 Python
python3使用GUI统计代码量
Sep 18 Python
python实现代码统计程序
Sep 19 Python
python实现文件批量编码转换及注意事项
Oct 14 Python
计算pytorch标准化(Normalize)所需要数据集的均值和方差实例
Jan 15 Python
使用pytorch搭建AlexNet操作(微调预训练模型及手动搭建)
Jan 18 Python
详解python的super()的作用和原理
Oct 29 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/12/29 PHP
在win系统安装配置 Memcached for PHP 5.3 图文教程
2015/03/03 PHP
利用php抓取蜘蛛爬虫痕迹的示例代码
2016/09/30 PHP
代码生成器 document.write()
2007/04/15 Javascript
jquery的$(document).ready()和onload的加载顺序
2010/05/26 Javascript
点弹代码 点击页面任何位置都可以弹出页面效果代码
2012/09/17 Javascript
node.js中的http.createServer方法使用说明
2014/12/14 Javascript
JS实现网页游戏中滑块响应鼠标点击移动效果
2015/10/19 Javascript
基于javascript实现简单的抽奖系统
2020/04/15 Javascript
浅谈几种常用的JS类定义方法
2016/06/08 Javascript
AngularJS过滤器filter用法实例分析
2016/11/04 Javascript
jQuery焦点图轮播效果实现方法
2016/12/19 Javascript
详解angularJs中自定义directive的数据交互
2017/01/13 Javascript
vue.js框架实现表单排序和分页效果
2017/08/09 Javascript
Angular7创建项目、组件、服务以及服务的使用
2019/02/19 Javascript
Vue 之孙组件向爷组件通信的实现
2019/04/23 Javascript
详解Typescript里的This的使用方法
2021/01/08 Javascript
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
Python中使用pprint函数进行格式化输出的教程
2015/04/07 Python
python中字典(Dictionary)用法实例详解
2015/05/30 Python
python 处理微信对账单数据的实例代码
2019/07/19 Python
python selenium xpath定位操作
2020/09/01 Python
使用postMessage让 iframe自适应高度的方法示例
2019/10/08 HTML / CSS
荷兰本土平价百货:HEMA
2017/10/23 全球购物
公司JAVA开发面试题
2015/04/02 面试题
Linux如何为某个操作添加别名
2015/02/05 面试题
青年创业培训欢迎词
2014/01/08 职场文书
高中微机老师自我鉴定
2014/02/16 职场文书
大学共青团员个人自我评价
2014/04/16 职场文书
幼师个人总结范文
2015/02/28 职场文书
大学四年个人总结
2015/03/03 职场文书
专家推荐信范文
2015/03/26 职场文书
病危通知单
2015/04/17 职场文书
美容院管理规章制度
2015/08/05 职场文书
2019森林防火宣传标语大全!
2019/07/03 职场文书
MySQL索引失效十种场景与优化方案
2023/05/08 MySQL