欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python2.7實現(xiàn)復(fù)制大量文件及文件夾資料

 更新時間:2019年08月31日 16:32:00   作者:neo_will_mvp  
這篇文章主要為大家詳細介紹了python2.7實現(xiàn)復(fù)制大量文件及文件夾資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

需求:拷大量數(shù)據(jù),發(fā)現(xiàn)有2000G,靠系統(tǒng)的復(fù)制功能怕是得好幾個小時,于是回來學(xué)一手操作,話不多說上代碼:

說明:CopyFiles1是可以將sourceDir連子目錄一起原樣復(fù)制到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):
#完全連子目錄也會復(fù)制好,美觀
 global copyFileCounts
 print(sourceDir )
 print("%s 當(dāng)前處理文件夾%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 復(fù)制完畢" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF))
   else:
    print ("%s %s 已存在,不重復(fù)復(fù)制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF))
 
  if os.path.isdir(sourceF):
   copyFiles(sourceF, targetF)
 
def CopyFiles2(dir):
 #會將目錄下所有文件都復(fù)制在一起,速度快,可以篩選文件
 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,"圖層文件被復(fù)制!")
 
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)
 
#實戰(zhàn)代碼
#!/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
 
#定義拷貝文件的函數(shù)
def copyFiles(sourceDir, targetDir):
 global copyFileCounts
 print (sourceDir )
 print ("%s 當(dāng)前處理文件夾%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):
 #創(chuàng)建目錄
 if not os.path.exists(targetDir):
 os.makedirs(targetDir)
 copyFileCounts += 1
 
 #文件不存在的話,或者存在但是大小存在差異不同,執(zhí)行完全覆蓋操作
 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) 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python合并ts文件至mp4格式及解密教程詳解

    Python合并ts文件至mp4格式及解密教程詳解

    m3u8準確來說是一種索引文件,使用m3u8文件實際上是通過它來解析對應(yīng)的放在服務(wù)器上的視頻網(wǎng)絡(luò)地址,從而實現(xiàn)在線播放。本文給大家介紹Python合并ts文件至mp4格式及解密教程,需要的朋友參考下吧
    2021-07-07
  • Pytorch創(chuàng)建張量的四種方法

    Pytorch創(chuàng)建張量的四種方法

    Pytorch創(chuàng)建張量的4種方法主要有:torch.Tensor()、torch.tensor()、torch.as_tensor()、torch.from_numpy(),本文通過實例代碼介紹Pytorch創(chuàng)建張量的四種方法,需要的朋友可以參考下
    2023-05-05
  • pandas 查詢函數(shù)query的用法說明

    pandas 查詢函數(shù)query的用法說明

    這篇文章主要介紹了pandas 查詢函數(shù)query的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python Numpy運行報錯IndexError與形狀不匹配的問題解決辦法

    Python Numpy運行報錯IndexError與形狀不匹配的問題解決辦法

    在使用Numpy進行數(shù)據(jù)處理和科學(xué)計算時,IndexError和形狀不匹配(Shape Mismatch)是常見的錯誤類型,這些錯誤通常發(fā)生在數(shù)組索引操作、數(shù)組運算或數(shù)組重塑時,本文將通過一個具體的例子來詳細分析這些錯誤的原因和解決辦法,需要的朋友可以參考下
    2024-07-07
  • Python練習(xí)之制作企業(yè)獎金計算器

    Python練習(xí)之制作企業(yè)獎金計算器

    在本篇博客中,我們將使用Python代碼解決一個企業(yè)獎金計算的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Python 裝飾器實現(xiàn)DRY(不重復(fù)代碼)原則

    Python 裝飾器實現(xiàn)DRY(不重復(fù)代碼)原則

    python的裝飾器就是一種代碼簡潔的手段,在函數(shù)和方法有改動時,使得改動量最小。這篇文章給大家介紹了Python 裝飾器實現(xiàn)DRY(不重復(fù)代碼)原則,感興趣的朋友一起看看吧
    2018-03-03
  • pytorch框架的詳細介紹與應(yīng)用詳解

    pytorch框架的詳細介紹與應(yīng)用詳解

    這篇文章主要介紹了pytorch框架的詳細介紹與應(yīng)用,Torch?是一個經(jīng)典的對多維矩陣數(shù)據(jù)進行操作的張量(tensor?)庫,在機器學(xué)習(xí)和其他數(shù)學(xué)密集型應(yīng)用有廣泛應(yīng)用,本文給大家詳細講解,需要的朋友可以參考下
    2023-04-04
  • Python找出9個連續(xù)的空閑端口

    Python找出9個連續(xù)的空閑端口

    這篇文章主要介紹了Python找出9個連續(xù)的空閑端口的方法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • pd.DataFrame中的幾種索引變換的實現(xiàn)

    pd.DataFrame中的幾種索引變換的實現(xiàn)

    本文主要介紹了pd.DataFrame中的幾種索引變換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Python使用Matplotlib繪制3D圣誕樹

    Python使用Matplotlib繪制3D圣誕樹

    這篇文章主要為大家詳細介紹了Python如何使用Matplotlib繪制3D圣誕樹,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12

最新評論