1. 程式人生 > 程式設計 >Python3 filecmp模組測試比較檔案原理解析

Python3 filecmp模組測試比較檔案原理解析

1.filecmp比較檔案

filecmp模組提供了一些函式和一個類來比較檔案系統上的檔案和目錄。

1.1 示例資料

使用下面程式碼建立一組測試檔案。

import os
def mkfile(filename,body=None):
  with open(filename,'w') as f:
    f.write(body or filename)
  return
 
def make_example_dir(top):
  if not os.path.exists(top):
    os.mkdir(top)
  curdir = os.getcwd()
  os.chdir(top)
  os.mkdir('dir1')
  os.mkdir('dir2')
  mkfile('dir1/file_only_in_dir1')
  mkfile('dir2/file_only_in_dir2')
  os.mkdir('dir1/dir_only_in_dir1')
  os.mkdir('dir2/dir_only_in_dir2')
  os.mkdir('dir1/common_dir')
  os.mkdir('dir2/common_dir')
  mkfile('dir1/common_file','this file is the same')
  os.link('dir1/common_file','dir2/common_file')
  mkfile('dir1/contents_differ')
  mkfile('dir2/contents_differ')
  # Update the access and modification times so most of the stat
  # results will match.
  st = os.stat('dir1/contents_differ')
  os.utime('dir2/contents_differ',(st.st_atime,st.st_mtime))
  mkfile('dir1/file_in_dir1','This is a file in dir1')
  os.mkdir('dir2/file_in_dir1')
  os.chdir(curdir)
  return
 
if __name__ == '__main__':
  os.chdir(os.path.dirname(__file__) or os.getcwd())
  make_example_dir('example')
  make_example_dir('example/dir1/common_dir')
  make_example_dir('example/dir2/common_dir')

執行這個指令碼會在axample目錄下生成一個檔案樹。

Python3 filecmp模組測試比較檔案原理解析

common_dir目錄下也有同樣的目錄結構,以提供有意思的遞迴比較選擇。

1.2 比較檔案

cmp()用於比較檔案系統上的兩個檔案。

import filecmp
print('common_file  :',end=' ')
print(filecmp.cmp('example/dir1/common_file','example/dir2/common_file',shallow=True),shallow=False))
print('contents_differ:',end=' ')
print(filecmp.cmp('example/dir1/contents_differ','example/dir2/contents_differ',shallow=False))
print('identical   :',end=' ')
print(filecmp.cmp('example/dir1/file_only_in_dir1','example/dir1/file_only_in_dir1',shallow=False))

shallo引數告訴cmp()除了檔案的元資料外,是否還要檢視檔案的內容。預設情況下,會使用由os.stat()得到的資訊來完成一個淺比較。如果結果是一樣的,則認為檔案相同。因此,對於同時建立的相同大小的檔案,即使他們的內容不同,也會報告為是相同的檔案。當shallow為False時,則要比較檔案的內容。

Python3 filecmp模組測試比較檔案原理解析

如果非遞迴的比較兩個目錄中的一組檔案,則可以使用cmpfiles()。引數是目錄名和兩個位置上要檢查的我就愛你列表。傳入的公共檔案列表應當只包含檔名(目錄會導致匹配不成功),而且這些檔案在兩個位置上都應當出現。下一個例子顯示了構造公共列表的一種簡單方法。與cmp()一樣,這個比較也有一個shallow標誌。

import filecmp
import os
# Determine the items that exist in both directories
d1_contents = set(os.listdir('example/dir1'))
d2_contents = set(os.listdir('example/dir2'))
common = list(d1_contents & d2_contents)
common_files = [
  f
  for f in common
  if os.path.isfile(os.path.join('example/dir1',f))
]
print('Common files:',common_files)
# Compare the directories
match,mismatch,errors = filecmp.cmpfiles(
  'example/dir1','example/dir2',common_files,)
print('Match    :',match)
print('Mismatch  :',mismatch)
print('Errors   :',errors)

cmpfiles()返回3個檔名列表,分別包含匹配的檔案、不匹配的檔案和不能比較的檔案(由於許可權問題或出於其他原因)。

Python3 filecmp模組測試比較檔案原理解析

1.3 比較目錄

前面介紹的函式適合完成相對簡單的比較。對於大目錄樹的遞迴比較或者更完整的分析,dircmp類很更有用。在最簡單的用例中,report()會列印比較兩個目錄的報告。

import filecmp
dc = filecmp.dircmp('example/dir1','example/dir2')
dc.report()

輸出是一個純文字報告,顯示的結果只包括給定目錄的內容,而不會遞迴比較其子目錄。在這裡,認為檔案not_the_same是相同的,因為這裡沒有比較內容。無法讓dircmp像cmp()那樣比較檔案的內容。

Python3 filecmp模組測試比較檔案原理解析

為了更多的細節,也為了完成一個遞迴比較,可以使用report_full_closure()。

import filecmp
dc = filecmp.dircmp('example/dir1','example/dir2')
dc.report_full_closure()

輸出將包括所有同級子目錄的比較。

Python3 filecmp模組測試比較檔案原理解析

1.4 在程式中使用差異

除了生成列印報告,dircmp還能計算檔案列表,可以在程式中直接使用。以下各個屬性只在請求時才計算,所以對於未用的資料,建立dircmp例項不會帶來開銷。

import filecmp
import pprint
dc = filecmp.dircmp('example/dir1','example/dir2')
print('Left:')
pprint.pprint(dc.left_list)
print('\nRight:')
pprint.pprint(dc.right_list)

所比較目錄中包含的檔案和子目錄分別列在left_list和right_list中。

Python3 filecmp模組測試比較檔案原理解析

可以向建構函式傳入一個要忽略的名字列表(該列表中指定的名字將被忽略)來對輸入進行過濾。預設的,RCS、CVS和tags等名字會被忽略。

import filecmp
import pprint
dc = filecmp.dircmp('example/dir1',ignore=['common_file'])
print('Left:')
pprint.pprint(dc.left_list)
print('\nRight:')
pprint.pprint(dc.right_list)

在這裡,將common_file從要比較的檔案列表中去除。

Python3 filecmp模組測試比較檔案原理解析

兩個輸入目錄中共有的檔名會儲存在common內,各目錄獨有的檔案會列在left_only和right_only中。

import filecmp
import pprint
dc = filecmp.dircmp('example/dir1','example/dir2')
print('Common:')
pprint.pprint(dc.common)
print('\nLeft:')
pprint.pprint(dc.left_only)
print('\nRight:')
pprint.pprint(dc.right_only)

"左"目錄是dircmp()的第一個引數,"右"目錄是第二個引數。

Python3 filecmp模組測試比較檔案原理解析

公共成員可以被進一步分解為檔案、目錄和“有趣”元素(兩個目錄中型別不同的內容,或者os.stat()指出的有錯誤的地方)。

import filecmp
import pprint
dc = filecmp.dircmp('example/dir1','example/dir2')
print('Common:')
pprint.pprint(dc.common)
print('\nDirectories:')
pprint.pprint(dc.common_dirs)
print('\nFiles:')
pprint.pprint(dc.common_files)
print('\nFunny:')
pprint.pprint(dc.common_funny)

在示例資料中,file_in_dir1元素在一個目錄中是一個檔案,而在另一個目錄中是一個子目錄,所以它會出現在“有趣”列表中。

Python3 filecmp模組測試比較檔案原理解析

檔案之間的差別也可以做類似的劃分。

import filecmp
dc = filecmp.dircmp('example/dir1','example/dir2')
print('Same   :',dc.same_files)
print('Different :',dc.diff_files)
print('Funny   :',dc.funny_files)

檔案not_the_same通過os.stat()比較,並且不檢查內容,所以它包含在same_files列表中。

Python3 filecmp模組測試比較檔案原理解析

最後一點,子目錄也會被儲存,以便容易地完成遞迴比較。

import filecmp
dc = filecmp.dircmp('example/dir1','example/dir2')
print('Subdirectories:')
print(dc.subdirs)

屬性subdirs是一個字典,它將目錄名對映到新的dircmp物件。

Python3 filecmp模組測試比較檔案原理解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。