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爬取微博数据生成词云图片实例代码
Aug 31 Python
1 行 Python 代码快速实现 FTP 服务器
Jan 25 Python
python 统计列表中不同元素的数量方法
Jun 29 Python
Python读取指定日期邮件的实例
Feb 01 Python
Python利用heapq实现一个优先级队列的方法
Feb 03 Python
Python常用模块之requests模块用法分析
May 15 Python
Python利用sqlacodegen自动生成ORM实体类示例
Jun 04 Python
Python中py文件转换成exe可执行文件的方法
Jun 14 Python
详解10个可以快速用Python进行数据分析的小技巧
Jun 24 Python
Python如何使用argparse模块处理命令行参数
Dec 11 Python
Python 发送邮件方法总结
Aug 10 Python
python 标准库原理与用法详解之os.path篇
Oct 24 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中strtotime函数使用方法详解
2011/11/27 PHP
用Php编写注册后Email激活验证的实例代码
2013/03/11 PHP
php中文乱码怎么办如何让浏览器自动识别utf-8
2014/01/15 PHP
php中time()与$_SERVER[REQUEST_TIME]用法区别
2014/11/19 PHP
php面象对象数据库操作类实例
2014/12/02 PHP
详解PHP执行定时任务的实现思路
2015/12/21 PHP
PHP封装的mysqli数据库操作类示例
2019/02/16 PHP
使用 TypeScript 重新编写的 JavaScript 坦克大战游戏代码
2015/04/07 Javascript
JavaScript html5 canvas绘制时钟效果(二)
2016/03/27 Javascript
详解基于angular-cli配置代理解决跨域请求问题
2017/07/05 Javascript
详解Vue2.x-directive的学习笔记
2017/07/17 Javascript
JS实现调用本地摄像头功能示例
2018/05/18 Javascript
javascript 模块依赖管理的本质深入详解
2020/04/30 Javascript
js 数据类型判断的方法
2020/12/03 Javascript
JS+CSS实现过渡特效
2021/01/02 Javascript
[59:30]完美世界DOTA2联赛PWL S3 access vs LBZS 第二场 12.20
2020/12/23 DOTA
python数据结构链表之单向链表(实例讲解)
2017/07/25 Python
Python开发SQLite3数据库相关操作详解【连接,查询,插入,更新,删除,关闭等】
2017/07/27 Python
matplotlib绘图实例演示标记路径
2018/01/23 Python
python、PyTorch图像读取与numpy转换实例
2020/01/13 Python
pandas和spark dataframe互相转换实例详解
2020/02/18 Python
基于python实现获取网页图片过程解析
2020/05/11 Python
PyInstaller运行原理及常用操作详解
2020/06/13 Python
使用HTML5的Canvas绘制曲线的简单方法
2015/09/08 HTML / CSS
美国现代家具购物网站:LexMod
2019/01/09 全球购物
Java程序员面试90题
2013/10/19 面试题
毕业评语大全
2014/05/04 职场文书
关于安全演讲稿
2014/05/09 职场文书
大学生求职信例文
2014/06/29 职场文书
我的梦想演讲稿500字
2014/08/21 职场文书
2016八一建军节慰问信
2015/11/30 职场文书
党风廉政承诺书2016
2016/03/25 职场文书
会计专业自荐信范文
2019/05/22 职场文书
vue backtop组件的实现完整代码
2021/04/07 Vue.js
解决jupyter notebook图片显示模糊和保存清晰图片的操作
2021/04/24 Python
vue实现可拖拽的dialog弹框
2021/05/13 Vue.js