ilecmp可以實現文件,目錄,遍歷子目錄的差異對比功能。
自帶filecmp模塊,無需安裝。
常用方法說明
filecmp提供3個操作方法,cmp(單文件對比),cmpfile(多文件對比),dircmp(目錄對比).
單文件對比,filecmp.cmp(f1,f2[,shallow])
f1 f2為文件,相同True,不同False,shallow默認為True,只根據os.stat()方法返回的文件基本信息進行對比。比如最后訪問時間,修改時間,狀態改變時間等,會忽略文件內容的對比,當shallow為False時,則os.stat()與文件內容同時進行校驗。
1 >>>filrcmp.cmp("/temp/a1","/temp/a2") 2 >>>True 3 4 >>>filrcmp.cmp("/temp/a1","/temp/a3") 5 >>>False
多文件對比,采用filecmp.cmpfiles(dir1, dir2, common[, shallow])
比較兩個文件夾內指定文件是否相等。參數dir1, dir2指定要比較的文件夾,參數common指定要比較的文件名列表。函數返回包含3個list元素的元組,分別表示匹配、不匹配以及錯誤的文件列表。錯誤的文件指的是不存在的文件,或文件被瑣定不可讀,或沒權限讀文件,或者由於其他原因訪問不了該文件。
目錄對比,通過 filecmp(a,b[,ignore[,hide]])類創建一個目錄比較對象
用於比較文件夾,通過該類比較兩個文件夾,可以獲取一些詳細的比較結果(如只在A文件夾存在的文件列表),並支持子文件夾的遞歸比較。
dircmp#提供了三個方法用於報告比較的結果: report():#只比較指定文件夾中的內容(文件與文件夾) report_partial_closure():#比較文件夾及第一級子文件夾的內容 report_full_closure():#遞歸比較所有的文件夾的內容
#dircmp還提供了下面這些屬性用於獲取比較的詳細結果 left_list:#左邊文件夾中的文件與文件夾列表; right_list:#右邊文件夾中的文件與文件夾列表; common:#兩邊文件夾中都存在的文件或文件夾; left_only:#只在左邊文件夾中存在的文件或文件夾; right_only:#只在右邊文件夾中存在的文件或文件夾; common_dirs:#兩邊文件夾都存在的子文件夾; common_files:#兩邊文件夾都存在的子文件; common_funny:#兩邊文件夾都存在的子文件夾; same_files:#匹配的文件; diff_files:#不匹配的文件; funny_files:#兩邊文件夾中都存在,但無法比較的文件; subdirs:#將common_dirs 目錄映射到新的dircmp對象,格式為字典的類型。
1 #!/usr/bin/python3 2 #coding:utf-8 3 import os,sys 4 import filecmp 5 import re 6 import shutil 7 ''' 8 校驗源與備份目錄的差異 9 ''' 10 11 holderlist = [] 12 def compareme(dir1,dir2): #遞歸獲取更新項函數 13 dircomp = filecmp.dircmp(dir1,dir2) 14 only_in_one = dircomp.left_only #源目錄新文件或目錄 15 diff_in_one = dircomp.diff_files #不匹配文件,源目錄文件已發生變化 16 dirpath = os.path.abspath(dir1) #定義源目錄絕對路徑 17 18 #將更新文件或目錄追加到holderlist 19 [ holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in only_in_one ] 20 [ holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diff_in_one ] 21 if len(dircomp.common_dirs) > 0: #判斷是否存在相同子目錄,以便遞歸 22 for item in dircomp.common_dirs: #遞歸子目錄 23 compareme(os.path.abspath(os.path.join(dir1,item)),os.path.abspath(os.path.join(dir2,item))) 24 return holderlist 25 26 def main(): 27 if len(sys.argv) > 2: #輸入源目錄與備份目錄 28 dir1 = sys.argv[1] 29 dir2 = sys.argv[2] 30 else : 31 print('Usage:',sys.argv[0],'datadir backdir') 32 sys.exit() 33 source_files = compareme(dir1,dir2) #對比源目錄與備份目錄 34 dir1 = os.path.abspath(dir1) #取絕對路徑后,后面不會自動加上'/' 35 36 if not dir2.endswith('/'): 37 dir2 = dir2+'/' #備份目錄路徑加'/' 38 39 dir2 = os.path.abspath(dir2) 40 destination_files = [] 41 createdir_bool = False 42 43 for item in source_files: #遍歷返回的差異文件或目錄清單 44 destination_dir = re.sub(dir1,dir2,item) #將源目錄差異路徑清單對應替換成備份目錄,即需要在dir2中創建的差異目錄和文件 45 destination_files.append(destination_dir) 46 if os.path.isdir(item): #如果差異路徑為目錄且不存在,則在備份目錄中創建 47 if not os.path.exists(destination_dir): 48 os.makedirs(destination_dir) 49 createdir_bool = True #再次調用copareme函數標記 50 if createdir_bool : #重新調用compareme函數,重新遍歷新創建目錄的內容 51 destination_files = [] 52 source_files = [] 53 source_files = compareme(dir1,dir2) #調用compareme函數 54 for item in source_files: #獲取源目錄差異路徑清單,對應替換成備份目錄 55 destination_dir = re.sub(dir1,dir2,item) 56 destination_files.append(destination_dir) 57 58 print('update item:') 59 print(source_files) #輸出更新項列表清單 60 copy_pair = zip(source_files,destination_files) #將源目錄與備份目錄文件清單拆分成元組 61 for item in copy_pair: 62 if os.path.isfile(item[0]): #判斷是否為文件,是則進行復制操作 63 shutil.copyfile(item[0],item[1]) 64 65 if __name__ == '__main__' : 66 main()