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的Bottle框架中返回静态文件和JSON对象的方法
Apr 30 Python
使用python编写简单的小程序编译成exe跑在win10上
Jan 15 Python
Tensorflow分类器项目自定义数据读入的实现
Feb 05 Python
简单了解Python生成器是什么
Jul 02 Python
用Python获取摄像头并实时控制人脸的实现示例
Jul 11 Python
python3的数据类型及数据类型转换实例详解
Aug 20 Python
Python时间差中seconds和total_seconds的区别详解
Dec 26 Python
python 实现控制鼠标键盘
Nov 27 Python
Python爬虫入门教程01之爬取豆瓣Top电影
Jan 24 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 Python
自己搭建resnet18网络并加载torchvision自带权重的操作
May 13 Python
利用python进行数据加载
Jun 20 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
超外差式晶体管收音机的组装与统调
2021/03/01 无线电
PHP 抓取新浪读书频道的小说并生成txt电子书的代码
2009/12/18 PHP
php插入排序法实现数组排序实例
2015/02/16 PHP
dvwa+xampp搭建显示乱码的问题及解决方案
2015/08/23 PHP
通过php动态传数据到highcharts
2017/04/05 PHP
javascript 控制弹出窗口
2007/04/10 Javascript
Jquery attr("checked") 返回checked或undefined 获取选中失效
2013/10/10 Javascript
js中AppendChild与insertBefore的用法详细解析
2013/12/16 Javascript
node.js实现多图片上传实例
2014/06/03 Javascript
JS仿百度自动下拉框模糊匹配提示
2016/07/25 Javascript
switch语句的妙用(必看篇)
2016/10/03 Javascript
jQuery旋转插件jqueryrotate用法详解
2016/10/13 Javascript
扩展Bootstrap Tooltip插件使其可交互的方法
2016/11/07 Javascript
微信小程序radio组件使用详解
2018/01/31 Javascript
layui实现动态和静态分页
2018/04/28 Javascript
vue-cli 使用vue-bus来全局控制的实例讲解
2018/09/15 Javascript
详解小程序退出页面时清除定时器
2019/04/28 Javascript
解决vue项目刷新后,导航菜单高亮显示的位置不对问题
2019/11/01 Javascript
Vue快速实现通用表单验证的方法
2020/02/24 Javascript
vue 输入电话号码自动按3-4-4分割功能的实现代码
2020/04/30 Javascript
实例分析javascript中的异步
2020/06/02 Javascript
python获取网页状态码示例
2014/03/30 Python
复习Python中的字符串知识点
2015/04/14 Python
讲解Python中fileno()方法的使用
2015/05/24 Python
利用Python抓取行政区划码的方法
2016/11/28 Python
Python连接SQLServer2000的方法详解
2017/04/19 Python
深入浅析Python的类
2018/06/22 Python
Python shutil模块用法实例分析
2019/10/02 Python
django rest framework 自定义返回方式
2020/07/12 Python
《童趣》教学反思
2014/02/19 职场文书
旅游市场营销方案
2014/03/09 职场文书
中国好声音华少广告词
2014/03/17 职场文书
幼儿园园务工作总结2015
2015/05/18 职场文书
人与自然的观后感
2015/06/18 职场文书
MySQL的安装与配置详细教程
2021/06/26 MySQL
MySQL 数据 data 基本操作
2022/05/04 MySQL