python同步兩個文件夾下的內(nèi)容
更新時間:2019年08月29日 11:31:51 作者:迦藍葉
這篇文章主要為大家詳細介紹了python同步兩個文件夾下的內(nèi)容,包括子文件夾,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python同步兩個文件夾下的內(nèi)容,供大家參考,具體內(nèi)容如下
import os import shutil import time import logging import filecmp #日志文件配置 log_filename ='synchro.log' #日志輸出格式化 log_format = '%(filename)s [%(asctime)s] [%(levelname)s] %(message)s' logging.basicConfig(format=log_format,datefmt='%Y-%m-%d %H:%M:%S %p',level=logging.DEBUG) #日志輸出到日志文件 fileLogger = logging.getLogger('fileLogger') fh = logging.FileHandler(log_filename) fh.setLevel(logging.INFO) fileLogger.addHandler(fh); #需要同步的文件夾路徑,可以使用絕對路徑,也可以使用相對路徑 synchroPath1 = r'/home/xxx/image1' synchroPath2 = r'/home/xxx/image2' #同步方法 def synchro(synchroPath1,synchroPath2): leftDiffList = filecmp.dircmp(synchroPath1,synchroPath2).left_only rightDiffList = filecmp.dircmp(synchroPath1,synchroPath2).right_only commondirsList =filecmp.dircmp(synchroPath1,synchroPath2).common_dirs for item in leftDiffList: copyPath = synchroPath1 + '/' + item pastePath = synchroPath2 + '/' + item if(os.path.isdir(copyPath)): copyDir(copyPath,pastePath) else : shutil.copy2(copyPath,pastePath) fileLogger.info('copy '+copyPath +" to "+pastePath) for item in rightDiffList: copyPath = synchroPath2 + '/' + item pastePath = synchroPath1 +'/' + item if(os.path.isdir(copyPath)): copyDir(copyPath,pastePath) else : shutil.copy2(copyPath,pastePath) fileLogger.info('copy '+copyPath +" to "+pastePath) for item in commondirsList: copyPath = synchroPath2 + '/' + item pastePath = synchroPath1 +'/' + item syncDir(copyPath,pastePath) #拷貝文件夾,如果文件夾不存在創(chuàng)建之后直接拷貝全部,如果文件夾已存在那么就同步文件夾 def copyDir(copyPath,pastePath): if(os.path.exists(pastePath)): synchro(copyPath,pastePath) else : os.mkdir(pastePath) shutil.copytree(copyPath,pastePath) #子文件夾左右兩側(cè)文件夾都包含,就同步兩側(cè)子文件夾 def syncDir(copyPath,pastePath): copyDir(copyPath,pastePath) copyDir(pastePath,copyPath) while(True): synchro(synchroPath1,synchroPath2) logging.debug('synchro run') #阻塞方法,上一步執(zhí)行結(jié)束后等待五秒 time.sleep(5)
代碼簡單,但是不優(yōu)雅,歡迎指正。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python讀取目錄下所有的jpg文件,并顯示第一張圖片的示例
今天小編就為大家分享一篇python讀取目錄下所有的jpg文件,并顯示第一張圖片的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python def函數(shù)的定義、使用及參數(shù)傳遞實現(xiàn)代碼
Python編程中對于某些需要重復(fù)調(diào)用的程序,可以使用函數(shù)進行定義,在Python中使用def用來定義函數(shù),這里簡單分享下, 方便學(xué)習(xí)python的朋友2014-08-08Python并行庫joblib之delayed函數(shù)與Parallel函數(shù)詳解
這篇文章主要介紹了Python并行庫joblib之delayed函數(shù)與Parallel函數(shù)詳解,Joblib就是一個可以簡單地將Python代碼轉(zhuǎn)換為并行計算模式的軟件包,它可非常簡單并行我們的程序,從而提高計算速度,需要的朋友可以參考下2023-08-08python中把元組轉(zhuǎn)換為namedtuple方法
在本篇文章里小編給大家整理的是一篇關(guān)于python中把元組轉(zhuǎn)換為namedtuple方法,有興趣的朋友們可以參考下。2020-12-12python使用pygame模塊實現(xiàn)坦克大戰(zhàn)游戲
這篇文章主要為大家詳細介紹了python使用pygame模塊實現(xiàn)坦克大戰(zhàn)游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05Flask框架學(xué)習(xí)筆記之使用Flask實現(xiàn)表單開發(fā)詳解
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之使用Flask實現(xiàn)表單開發(fā),結(jié)合實例形式較為詳細的分析了flask框架表單模板定義、數(shù)據(jù)提交等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08