本文實(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()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)標(biāo)題:Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能示例-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.chinadenli.net/article46/djhoeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、做網(wǎng)站、域名注冊(cè)、關(guān)鍵詞優(yōu)化、網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容