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中实现php的var_dump函数功能
Jan 21 Python
对Python新手编程过程中如何规避一些常见问题的建议
Apr 01 Python
Python获取文件所在目录和文件名的方法
Jan 12 Python
python获取当前用户的主目录路径方法(推荐)
Jan 12 Python
python使用xslt提取网页数据的方法
Feb 23 Python
详解Python logging调用Logger.info方法的处理过程
Feb 12 Python
浅谈pyqt5在QMainWindow中布局的问题
Jun 21 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
python实现可下载音乐的音乐播放器
Feb 25 Python
Django程序的优化技巧
Apr 29 Python
Python 发送SMTP邮件的简单教程
Jun 24 Python
Python爬取奶茶店数据分析哪家最好喝以及性价比
Sep 23 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
基于数据库的在线人数,日访问量等统计
2006/10/09 PHP
PHP多线程批量采集下载美女图片的实现代码(续)
2013/06/03 PHP
浅谈php中curl、fsockopen的应用
2016/12/10 PHP
插件:检测javascript的内存泄漏
2007/03/04 Javascript
GWT中复制到剪贴板 js+flash实现复制 兼容性比较好
2010/03/07 Javascript
js中点击空白区域时文本框与隐藏层的显示与影藏问题
2013/08/26 Javascript
js实现头像图片切割缩放及无刷新上传图片的方法
2015/07/17 Javascript
jquery实现多条件筛选特效代码分享
2015/08/28 Javascript
Nodejs如何复制文件
2016/03/09 NodeJs
js实现添加可信站点、修改activex安全设置,禁用弹出窗口阻止程序
2016/08/17 Javascript
使用BootStrap实现表格隔行变色及hover变色并在需要时出现滚动条
2017/01/04 Javascript
Vue filters过滤器的使用方法
2017/07/14 Javascript
JS将时间秒转换成天小时分钟秒的字符串
2019/07/10 Javascript
在vue中高德地图引入和轨迹的绘制的实现
2019/10/11 Javascript
微信小程序自定义tabbar custom-tab-bar 6s出不来解决方案(cover-view不兼容)
2019/11/01 Javascript
如何在JS文件中获取Vue组件
2020/09/16 Javascript
微信小程序实现锚点跳转
2020/11/23 Javascript
微信小程序用户登录和登录态维护的实现
2020/12/10 Javascript
Python中如何优雅的合并两个字典(dict)方法示例
2017/08/09 Python
CentOS 7下安装Python3.6 及遇到的问题小结
2018/11/08 Python
Keras Convolution1D与Convolution2D区别说明
2020/05/22 Python
Pandas对每个分组应用apply函数的实现
2020/12/13 Python
image-set实现Retina屏幕下图片显示详细介绍
2012/12/24 HTML / CSS
深入解析HTML5中的Blob对象的使用
2015/09/08 HTML / CSS
应届生煤化工求职信
2013/10/21 职场文书
中英文自我评价语句
2013/12/20 职场文书
汽车维修专业个人求职信范文
2014/01/01 职场文书
《奇妙的国际互联网》 教学反思
2014/02/25 职场文书
珍惜资源保护环境的建议书
2014/05/14 职场文书
学术研讨会主持词
2015/07/04 职场文书
学校隐患排查制度
2015/08/05 职场文书
《你在为谁工作》心得体会(共8篇)
2016/01/20 职场文书
导游词之岳阳楼
2019/09/25 职场文书
Nest.js参数校验和自定义返回数据格式详解
2021/03/29 Javascript
实例讲解Python中sys.argv[]的用法
2021/06/03 Python
python中__slots__节约内存的具体做法
2021/07/04 Python