Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能。分享給大家供大家參考,具體如下:
在項(xiàng)目中,經(jīng)常要更新文件,在更新之前首先要備份源文件,所以就用到了這個(gè)腳本(來(lái)自于Python自動(dòng)化運(yùn)維這本書),總共有以下幾個(gè)步驟:
1. 獲取要進(jìn)行比較的兩個(gè)目錄,進(jìn)行差異比較,把源目錄特有的文件或目錄、以及和備份目錄不同的文件或目錄保存到列表中,并且判斷目錄下面是否還有目錄,遞歸進(jìn)行保存這些差異文件。
2. 將差異文件列表中文件或目錄的路徑換成對(duì)應(yīng)的備份路徑,進(jìn)行判斷,如果備份路徑不存在,就創(chuàng)建目錄。
3. 繼續(xù)對(duì)比源目錄和新創(chuàng)建的備份目錄中的差異文件,把源路徑換成備份目錄的路徑。
4. 然后遍歷復(fù)制源目錄文件到備份目錄。
以下是具體的實(shí)現(xiàn)代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import filecmp
import re
import shutil
holderlist = []
##對(duì)應(yīng)第一個(gè)步驟
def compare_me(dir1, dir2):
dircomp = filecmp.dircmp(dir1, dir2)
only_in_one = dircomp.left_only
diff_in_one = dircomp.diff_files
dirpath = os.path.abspath(dir1)
[ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in only_in_one ]
[ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in diff_in_one ]
if len(dircomp.common_dirs) > 0:
for item in dircomp.common_dirs:
compare_me(os.path.abspath(os.path.join(dir1, item)), os.path.abspath(os.path.join(dir2, item)))
return holderlist
##對(duì)應(yīng)第二個(gè)步驟
def main():
if len(sys.argv) > 2:
dir1 = sys.argv[1]
dir2 = sys.argv[2]
else:
print "Usage: ", sys.argv[0], "datadir backupdir"
sys.exit()
source_files = compare_me(dir1, dir2)
dir1 = os.path.abspath(dir1)
if not dir2.endswith('/'):
dir2 = dir2 + '/'
dir2 = os.path.abspath(dir2)
destination_files = []
createdir_bool = False
for item in source_files:
destination_dir = re.sub(dir1, dir2, item)
destination_files.append(destination_dir)
if os.path.isdir(item):
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
createdir_bool = True
##對(duì)應(yīng)第三個(gè)步驟
if createdir_bool:
destination_files = []
source_files = []
source_files = compare_me(dir1, dir2)
for item in source_files:
destination_dir = re.sub(dir1, dir2, item)
destination_files.append(destination_dir)
##對(duì)應(yīng)第四個(gè)步驟
print "update item: "
print source_files
copy_pair = zip(source_files, destination_files)
print "copy_pair is %s" % copy_pair
for item in copy_pair:
print "item is %s, %s" % (item[0], item[1])
if os.path.isfile(item[0]):
shutil.copyfile(item[0], item[1])
if __name__ == '__main__':
main()
最后根據(jù)需要,可以設(shè)定一個(gè)定時(shí)檢查,進(jìn)行自動(dòng)同步源目錄和備份目錄,讓其保持一致性。
PS:這里再為大家推薦一款功能相似的在線工具供大家參考使用:
在線文本比較工具:
http://tools.jb51.net/aideddesign/txt_diff
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python基礎(chǔ)之模塊相關(guān)知識(shí)總結(jié)
今天帶大家復(fù)習(xí)Python基礎(chǔ)知識(shí),文中對(duì)模塊相關(guān)知識(shí)介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
構(gòu)建高效的python requests長(zhǎng)連接池詳解
這篇文章主要介紹了構(gòu)建高效的python requests長(zhǎng)連接池詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python3報(bào)錯(cuò)check_hostname?requires?server_hostname的解決
這篇文章主要介紹了python3報(bào)錯(cuò)check_hostname?requires?server_hostname的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Python實(shí)現(xiàn)簡(jiǎn)單圖像縮放與旋轉(zhuǎn)
大家好,本篇文章主要講的是Python實(shí)現(xiàn)簡(jiǎn)單圖像縮放與旋轉(zhuǎn),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Python隨機(jī)讀取文件實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Python隨機(jī)讀取文件的相關(guān)資料,需要的朋友可以參考下2017-05-05
python3中set(集合)的語(yǔ)法總結(jié)分享
這篇文章主要總結(jié)了關(guān)于python3中set(集合)的語(yǔ)法的相關(guān)資料,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
Python入門開(kāi)發(fā)教程 windows下搭建開(kāi)發(fā)環(huán)境vscode的步驟詳解
大家都知道Python是跨平臺(tái)的,它可以運(yùn)行在Windows、Mac和各種Linux/Unix系統(tǒng)上。在Windows上寫Python程序,放到Linux上也是能夠運(yùn)行的,今天給大家分享Python開(kāi)發(fā)環(huán)境搭建vscode的步驟,一起看看吧2021-07-07

