python實現(xiàn)復(fù)制大量文件功能
本文實例為大家分享了python實現(xiàn)復(fù)制大量文件的具體代碼,供大家參考,具體內(nèi)容如下
本來是去項目公司拷數(shù)據(jù),結(jié)果去了發(fā)現(xiàn)有500G,靠系統(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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
安裝Pycharm2019以及配置anconda教程的方法步驟
這篇文章主要介紹了安裝Pycharm2019以及配置anconda教程的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python中(str,list,tuple)基礎(chǔ)知識匯總
本文給大家匯總介紹的是python中str(字符串)、list(列表)、tuple(元組)、dict(字典)的一些基礎(chǔ)知識,有需要的小伙伴可以參考下2018-02-02python 動態(tài)遷移solr數(shù)據(jù)過程解析
這篇文章主要介紹了python 動態(tài)遷移solr數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Python使用tkinter庫實現(xiàn)文本顯示用戶輸入功能示例
這篇文章主要介紹了Python使用tkinter庫實現(xiàn)文本顯示用戶輸入功能,結(jié)合實例形式分析了tkinter庫獲取用戶輸入及控件顯示相關(guān)操作技巧,需要的朋友可以參考下2018-05-05django學(xué)習(xí)之a(chǎn)jax post傳參的2種格式實例
AJAX除了異步的特點外,還有一個就是:瀏覽器頁面局部刷新,下面這篇文章主要給大家介紹了關(guān)于django學(xué)習(xí)之a(chǎn)jax post傳參的2種格式的相關(guān)資料,需要的朋友可以參考下2021-05-05關(guān)于Django框架的關(guān)系模型序列化和一對多關(guān)系中的序列化解析
序列化的意思是把字典的形式轉(zhuǎn)化成Json格式。當(dāng)我們展示數(shù)據(jù)的時候需要使用,反序列化的話,就是Json轉(zhuǎn)成字典形式,存儲數(shù)據(jù)時候使用,需要的朋友可以參考下2023-05-05