如何使用python2.7复制大量文件

  介绍

如何使用python2.7复制大量文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

Python主要用来做什么

Python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。

import 操作系统   import 时间   import  shutil   时间=sourceDir  r" D: \小\ datatest"   时间=targetDir  r" D: \小\ result"   copyFileCounts =0   ,   def  CopyFiles1 (sourceDir, targetDir):   #完全连子目录也会复制好,美观   global  copyFileCounts   ,打印(sourceDir )   ,打印(“% s 当前处理文件夹% s已处理% s 个文件“,% (time.strftime (& # 39; Y % - % - % d  % H: % m: % & # 39;, time.localtime (time.time ())),, sourceDir, copyFileCounts),)   ,for  f  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),或(os.path.exists (targetF),以及(os.path.getsize (targetF), !=, os.path.getsize (sourceF))):   ,   ,,,开放(targetF,“wb") .write(打开(sourceF,“rb") .read ())   ,,,print  (“% s  % s 复制完毕“,% (time.strftime (& # 39; Y % - % - % d  % H: % m: % & # 39;, time.localtime (time.time ())),, targetF))   ,,:   ,,,print  (“% s  % s 已存在,不重复复制“,% (time.strftime (& # 39; Y % - % - % d  % H: % m: % & # 39;, time.localtime (time.time ())),, targetF))   ,   if 才能os.path.isdir (sourceF):   ,,复制文件(sourceF, targetF)   ,   def  CopyFiles2 (dir):   ,#会将目录下所有文件都复制在一起,速度快,可以筛选文件   i=0   ,for 根,dir1 filename 拷贝os.walk (dir):   #才能打印(文件名)   for 才能;index 拷贝范围(len(文件名)):   #才能打印(os.path.splitext(文件名(指数))[1])   # if 才能os.path.splitext(文件名(指数))[1]==& # 39;# 39;公司:#这里注意文件名是个元组,splitext方法的时候只能是字符串   if 才能;1==1:   ,,#我+=1   ,,print() & # 39;这是# 39;   ,,root1=癉: \ \小\ \ result3"   ,,old_path =, os.path.join(根,,文件名(指数))   ,才能打印(old_path)   ,,new_path =, os.path.join (root1文件名(指数))   ,,shutil.copyfile (old_path new_path)   ,   #打印(“总共有“,我,“图层文件被复制!“)   ,   if  __name__ ==,“__main__":=,,time_start  time.time ()   ,试一试:   import  psyco   ,psyco.profile ()   ,except  ImportError:   ,通过   ,# CopyFiles1 (sourceDir targetDir)   ,CopyFiles2 (“D:/?datatest")=,,time_end  time.time ()   ,打印(& # 39;totally 成本# 39;,,time_end 作用;time_start)   ,   #实战代码   # !/usr/bin/python2   #=utf - 8编码   # @author  neo_will   # version  2019 - 04 - 02, 39   ,   import 操作系统   import  os.path   import  shutil   import 时间,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 \上海\ 2018 \ fpath_2018 [0:]“;   #原始目录和拷贝到的目录地址   sourceDir =, r" D: \ \ wj"工具;   时间=targetDir  r" D: \ \ wj"用户;   copyFileCounts =0   ,   #定义拷贝文件的函数   def 拷贝文件(sourceDir, targetDir):   global  copyFileCounts   ,print  (sourceDir )   ,print  (“% s 当前处理文件夹% s已处理% s 个文件“,% (time.strftime (& # 39; Y % - % - % d  % H: % m: % & # 39;, time.localtime (time.time ())),, sourceDir, copyFileCounts),)   ,for  f  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),或(os.path.exists (targetF),以及(os.path.getsize (targetF), !=, os.path.getsize (sourceF))):   ,#二进制文件   ,打开(targetF,“wb") .write(打开(sourceF,“rb") .read ())   ,print  u" % s  % s  copy  over", % (time.strftime (& # 39; Y % - % - % d  % H: % m: % & # 39;, time.localtime (time.time ())),, targetF)   ,其他的:   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何使用python2.7复制大量文件