ilecmp可以實作檔案,目錄,周遊子目錄的差異對比功能。
自帶filecmp子產品,無需安裝。
常用方法說明
filecmp提供3個操作方法,cmp(單檔案對比),cmpfile(多檔案對比),dircmp(目錄對比).
單檔案對比,filecmp.cmp(f1,f2[,shallow])
f1 f2為檔案,相同True,不同False,shallow預設為True,隻根據os.stat()方法傳回的檔案基本資訊進行對比。比如最後通路時間,修改時間,狀态改變時間等,會忽略檔案内容的對比,當shallow為False時,則os.stat()與檔案内容同時進行校驗。
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 >>>filrcmp.cmp("/temp/a1","/temp/a2") 2 >>>True 3
4 >>>filrcmp.cmp("/temp/a1","/temp/a3") 5 >>>False</pre>
多檔案對比,采用filecmp.cmpfiles(dir1, dir2, common[, shallow])
比較兩個檔案夾内指定檔案是否相等。參數dir1, dir2指定要比較的檔案夾,參數common指定要比較的檔案名清單。函數傳回包含3個list元素的元組,分别表示比對、不比對以及錯誤的檔案清單。錯誤的檔案指的是不存在的檔案,或檔案被瑣定不可讀,或沒權限讀檔案,或者由于其他原因通路不了該檔案。
目錄對比,通過 filecmp(a,b[,ignore[,hide]])類建立一個目錄比較對象
用于比較檔案夾,通過該類比較兩個檔案夾,可以擷取一些詳細的比較結果(如隻在A檔案夾存在的檔案清單),并支援子檔案夾的遞歸比較。
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">dircmp#提供了三個方法用于報告比較的結果:
report():#隻比較指定檔案夾中的内容(檔案與檔案夾)
report_partial_closure():#比較檔案夾及第一級子檔案夾的内容
report_full_closure():#遞歸比較所有的檔案夾的内容</pre>
[
複制代碼
](javascript:void(0); "複制代碼")
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#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對象,格式為字典的類型。</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 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()</pre>