新聞中心
這里有您想知道的互聯網營銷解決方案
python怎樣實現復制目錄到指定目錄-創(chuàng)新互聯
這篇文章將為大家詳細講解有關python怎樣實現復制目錄到指定目錄,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
python復制目錄到指定目錄的具體代碼如下
保存下面代碼為一個文件直接運行
import os import time copyFileCounts = 0 def copyFiles(sourceDir, targetDir): global copyFileCounts print (sourceDir) print (u"%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): #創(chuàng)建目錄 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))): #2進制文件 open(targetF, "wb").write(open(sourceF, "rb").read()) print (u"%s %s 復制完畢" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)) else: print (u"%s %s 已存在,不重復復制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)) if os.path.isdir(sourceF): copyFiles(sourceF, targetF) if __name__ == "__main__": copyFiles('/content/chest_xray/', '/content/drive/My Drive/chest_xray/')
刪除非空目錄的所有有文件,包含目錄本身
import shutil shutil.rmtree('D:/content/')
關于“python怎樣實現復制目錄到指定目錄”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁標題:python怎樣實現復制目錄到指定目錄-創(chuàng)新互聯
URL網址:http://www.ef60e0e.cn/article/decdjc.html