python同步兩個(gè)文件夾下的內(nèi)容
更新時(shí)間:2019年08月29日 11:31:51 作者:迦藍(lán)葉
這篇文章主要為大家詳細(xì)介紹了python同步兩個(gè)文件夾下的內(nèi)容,包括子文件夾,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python同步兩個(gè)文件夾下的內(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);
#需要同步的文件夾路徑,可以使用絕對(duì)路徑,也可以使用相對(duì)路徑
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)
代碼簡(jiǎn)單,但是不優(yōu)雅,歡迎指正。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用OpenCV對(duì)圖像進(jìn)行縮放功能
這篇文章主要介紹了Python使用OpenCV對(duì)圖像進(jìn)行縮放功能,文中給大家提到了兩種調(diào)用方式及OpenCV中pryUp和pryDown的用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Laravel+Dingo/Api 自定義響應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了Laravel+Dingo/Api 自定義響應(yīng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
CentOS 6.5中安裝Python 3.6.2的方法步驟
centos 6.5默認(rèn)自帶的python版本為2.6,而下面這篇文章主要給大家介紹了關(guān)于在CentOS 6.5中安裝Python 3.6.2的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
pandas中的數(shù)據(jù)去重處理的實(shí)現(xiàn)方法
這篇文章主要介紹了pandas中的數(shù)據(jù)去重處理的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
樹(shù)莓派使用python-librtmp實(shí)現(xiàn)rtmp推流h264的方法
今天小編就為大家分享一篇樹(shù)莓派使用python-librtmp實(shí)現(xiàn)rtmp推流h264的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07

