python如何實現(xiàn)復制目錄到指定目錄
更新時間:2020年02月13日 10:38:13 作者:super_xxt
這篇文章主要為大家介紹了python如何實現(xiàn)復制目錄到指定目錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python復制目錄到指定目錄的具體代碼,供大家參考,具體內(nèi)容如下
保存下面代碼為一個文件直接運行
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/')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python 獲取指定文件夾下的目錄和文件的實現(xiàn)
這篇文章主要介紹了Python 獲取指定文件夾下的目錄和文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08