python2.7實現複製大量文件及文件夾資料

這篇文章主要爲大家詳細介紹了python2.7實現複製大量文件及文件夾資料,具有一定的參考價值,感興趣的小夥伴們可以參考一下

需求:拷大量數據,發現有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) 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章