如何使用python2.7復(fù)制大量文件?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。
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))): #二進(jìn)制文件 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)
關(guān)于如何使用python2.7復(fù)制大量文件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:如何使用python2.7復(fù)制大量文件-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://jinyejixie.com/article16/depegg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)