確認備份目錄與源目錄檔案是否保持一致,包括源目錄中的新檔案或目錄、刪除檔案或刪除目錄、更新檔案或目錄有無成功同步,定期校驗,保證源目錄和備份目錄的同步,對相同檔案不做任何改動,對更新檔案或目錄、新檔案或目錄進行拷貝更新,對源目錄刪除檔案或目錄在備份目錄中刪除
使用filecmp模組的left_only,diff_files等獲取更新專案,right_only獲取源目錄中刪除項
使用命令find dir1 -type f -print0 | xargs -0 md5sum快捷目錄清單對比
def compare(source, backup): # 遞迴獲取更新專案函式
uplist =
dellist =
dircomp = filecmp.dircmp(source, backup)
only_in_left = dircomp.left_only # 源目錄新檔案或目錄
only_in_right = dircomp.right_only # 源目錄刪除了的檔案
diff_in_one = dircomp.diff_files # 不匹配的檔案,源目錄檔案發生修改
dirpath = os.path
.abspath(source)
.abspath(os.path
.join(source, x))) for x
in only_in_left \
if os.path
.isfile(os.path
.abspath(os.path
.join(source, x)))]
.abspath(os.path
.join(source, x))) for x
in diff_in_one \
if os.path
.isfile(os.path
.abspath(os.path
.join(source, x)))]
.abspath(os.path
.join(backup, x))) for x
in only_in_right \
if os.path
.isfile(os.path
.abspath(os.path
.join(backup, x)))]
.abspath(os.path
.join(source, x))) for x
in only_in_left \
if os.path
.isdir(os.path
.abspath(os.path
.join(source, x)))]
.abspath(os.path
.join(backup, x))) for x
in only_in_right \
if os.path
.isdir(os.path
.abspath(os.path
.join(backup, x)))]
if len(dircomp.common_dirs) > 0:
for item in dircomp.common_dirs:
compare(os.path
.abspath(os.path
.join(source, item)), \
os.path
.abspath(os.path
.join(backup, item)))
if len(uplist) > 0:
for item in uplist:
getdir(item, source, backup)
if len(dellist) > 0:
for item in dellist:
result =
return result
def getdir(path, source, backup):
dirpath = os.path
.abspath(path)
temp = os.listdir(path)
dirs =
.abspath(os.path
.join(source, x))) for x
in temp \
if os.path
.isfile(os.path
.abspath(os.path
.join(path, x)))]
.abspath(os.path
.join(path, x))) for x
in temp \
if os.path
.isdir(os.path
.abspath(os.path
.join(path, x)))]
for item in dirs:
getdir(item, source, backup)
def update(source, backup, updatefiles, updatedirs):
source_path = os.path
.abspath(source)
print("begin create new directories...")
for item in updatedirs:
os.makedirs(item)
print("begin update files...")
for item in updatefiles:
backfile = re.sub(source, backup, item)
shutil.copyfile(item, backfile)
def delete(deletefiles, deletedirs):
print("begin to delete file...")
for item in deletefiles:
os.remove(item)
print("begin to delete directories...")
for item in deletedirs:
shutil.rmtree(item)
pass
def main():
if len(sys.argv) > 2:
dir1 = sys.argv[1]
dir2 = sys.argv[2]
if not dir1.endswith("/"): dir1 = dir1 + "/"
if not dir2.endswith("/"): dir2 = dir2 + "/"
else:
print("usage: " + sys.argv[0] + "datadir backupdir")
sys.exit()
result = compare(dir1, dir2)
updatefiles = result["updatefiles"]
updatedirs = result["updatedirs"]
deletefiles = result["deletefiles"]
deletedirs = result["deletedirs"]
delete(deletefiles, deletedirs)
update(dir1, dir2, updatefiles, updatedirs)
print("files need to update:" + str(updatefiles))
print("directory need to create:" + str(updatedirs))
print("files need to delete:" + str(deletefiles))
print("directory need to delete:" + str(deletedirs))
if __name__ == "__main__":
main()
執行指令碼之後
python模組詳解 filecmp
簡介 filecmp是python內建的乙個模組,用於比較檔案及資料夾的內容,它是乙個輕量級的工具,使用非常簡單 兩個主要的方法 比較兩個檔案的內容是否匹配。引數f1,f2指定要比較的檔案的路徑。可選引數shallow指定比較檔案時是否需要考慮檔案本身的屬性 通過os.stat函式可以獲得檔案屬性 ...
Python模組filecmp 檔案比較
filecmp模組用於比較檔案及資料夾的內容,它是乙個輕量級的工具,使用非常簡單。python標準庫還提供了difflib模組用於比較檔案的內容。關於difflib模組,且聽下回分解。filecmp定義了兩個函式,用於方便地比較檔案與資料夾 filecmp.cmp f1,f2 shallow 比較兩...
通過Python模組filecmp 對檔案比較
filecmp定義了兩個函式,用於方便地比較檔案與資料夾 filecmp.cmp f1,f2 shallow 比較兩個檔案的內容是否匹配。引數f1,f2指定要比較的檔案的路徑。可選引數shallow指定比較檔案時是否需要考慮檔案本身的屬性 通過 os.stat函式可以獲得檔案屬性 如果檔案內容匹配,...