最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3 filecmp模塊測試比較文件原理解析

 更新時間:2020年03月23日 15:42:51   作者:愛編程的小灰灰  
這篇文章主要介紹了Python3 filecmp模塊測試比較文件原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1.filecmp比較文件

filecmp模塊提供了一些函數(shù)和一個類來比較文件系統(tǒng)上的文件和目錄。

1.1 示例數(shù)據(jù)

使用下面代碼創(chuàng)建一組測試文件。

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目錄下生成一個文件樹。

common_dir目錄下也有同樣的目錄結(jié)構(gòu),以提供有意思的遞歸比較選擇。

1.2 比較文件

cmp()用于比較文件系統(tǒng)上的兩個文件。

import filecmp
print('common_file  :', end=' ')
print(filecmp.cmp('example/dir1/common_file',
         'example/dir2/common_file',
         shallow=True),
   end=' ')
print(filecmp.cmp('example/dir1/common_file',
         'example/dir2/common_file',
         shallow=False))
print('contents_differ:', end=' ')
print(filecmp.cmp('example/dir1/contents_differ',
         'example/dir2/contents_differ',
         shallow=True),
   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=True),
   end=' ')
print(filecmp.cmp('example/dir1/file_only_in_dir1',
         'example/dir1/file_only_in_dir1',
         shallow=False))

shallo參數(shù)告訴cmp()除了文件的元數(shù)據(jù)外,是否還要查看文件的內(nèi)容。默認(rèn)情況下,會使用由os.stat()得到的信息來完成一個淺比較。如果結(jié)果是一樣的,則認(rèn)為文件相同。因此,對于同時創(chuàng)建的相同大小的文件,即使他們的內(nèi)容不同,也會報告為是相同的文件。當(dāng)shallow為False時,則要比較文件的內(nèi)容。

如果非遞歸的比較兩個目錄中的一組文件,則可以使用cmpfiles()。參數(shù)是目錄名和兩個位置上要檢查的我就愛你列表。傳入的公共文件列表應(yīng)當(dāng)只包含文件名(目錄會導(dǎo)致匹配不成功),而且這些文件在兩個位置上都應(yīng)當(dāng)出現(xiàn)。下一個例子顯示了構(gòu)造公共列表的一種簡單方法。與cmp()一樣,這個比較也有一個shallow標(biāo)志。

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個文件名列表,分別包含匹配的文件、不匹配的文件和不能比較的文件(由于權(quán)限問題或出于其他原因)。

1.3 比較目錄

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

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

輸出是一個純文本報告,顯示的結(jié)果只包括給定目錄的內(nèi)容,而不會遞歸比較其子目錄。在這里,認(rèn)為文件not_the_same是相同的,因為這里沒有比較內(nèi)容。無法讓dircmp像cmp()那樣比較文件的內(nèi)容。

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

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

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

1.4 在程序中使用差異

除了生成打印報告,dircmp還能計算文件列表,可以在程序中直接使用。以下各個屬性只在請求時才計算,所以對于未用的數(shù)據(jù),創(chuàng)建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中。

可以向構(gòu)造函數(shù)傳入一個要忽略的名字列表(該列表中指定的名字將被忽略)來對輸入進(jìn)行過濾。默認(rèn)的,RCS、CVS和tags等名字會被忽略。

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

在這里,將common_file從要比較的文件列表中去除。

兩個輸入目錄中共有的文件名會保存在common內(nèi),各目錄獨有的文件會列在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()的第一個參數(shù),"右"目錄是第二個參數(shù)。

公共成員可以被進(jìn)一步分解為文件、目錄和“有趣”元素(兩個目錄中類型不同的內(nèi)容,或者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)

在示例數(shù)據(jù)中,file_in_dir1元素在一個目錄中是一個文件,而在另一個目錄中是一個子目錄,所以它會出現(xiàn)在“有趣”列表中。

文件之間的差別也可以做類似的劃分。

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()比較,并且不檢查內(nèi)容,所以它包含在same_files列表中。

最后一點,子目錄也會被保存,以便容易地完成遞歸比較。

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中使用pyhook實現(xiàn)鍵盤監(jiān)控的例子

    python中使用pyhook實現(xiàn)鍵盤監(jiān)控的例子

    這篇文章主要介紹了python中使用pyhook實現(xiàn)鍵盤監(jiān)控的例子,包含pyhook的下載地址和手冊地址及一個Windows下的監(jiān)控實例,需要的朋友可以參考下
    2014-07-07
  • 從0到1使用python開發(fā)一個半自動答題小程序的實現(xiàn)

    從0到1使用python開發(fā)一個半自動答題小程序的實現(xiàn)

    這篇文章主要介紹了從0到1使用python開發(fā)一個半自動答題小程序的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 一文教你如何創(chuàng)建Python虛擬環(huán)境venv

    一文教你如何創(chuàng)建Python虛擬環(huán)境venv

    創(chuàng)建?Python?虛擬環(huán)境是一個很好的實踐,可以幫助我們管理項目的依賴項,避免不同項目之間的沖突,下面就跟隨小編一起學(xué)習(xí)一下如何創(chuàng)建Python虛擬環(huán)境venv吧
    2024-12-12
  • python3中宏HAVE_VFORK的使用

    python3中宏HAVE_VFORK的使用

    本文主要介紹了python3中宏HAVE_VFORK的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • python結(jié)合shell查詢google關(guān)鍵詞排名的實現(xiàn)代碼

    python結(jié)合shell查詢google關(guān)鍵詞排名的實現(xiàn)代碼

    這篇文章主要介紹了python結(jié)合shell查詢google關(guān)鍵詞排名的實現(xiàn)代碼,需要的朋友可以參考下
    2016-02-02
  • 對python3標(biāo)準(zhǔn)庫httpclient的使用詳解

    對python3標(biāo)準(zhǔn)庫httpclient的使用詳解

    今天小編就為大家分享一篇對python3標(biāo)準(zhǔn)庫httpclient的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • pycharm快捷鍵匯總

    pycharm快捷鍵匯總

    本文給大家分享的是PyCharm開發(fā)工具的快捷鍵大全整理,非常詳細(xì),適合使用PyCharm作為開發(fā)工具的開發(fā)人員參考使用,能夠幫助提高開發(fā)效率和速度
    2020-02-02
  • Python datetime模塊使用方法小結(jié)

    Python datetime模塊使用方法小結(jié)

    這篇文章主要介紹了Python datetime模塊使用方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Python入門教程(十七)Python的While循環(huán)

    Python入門教程(十七)Python的While循環(huán)

    這篇文章主要介紹了Python入門教程(十七)Python的While循環(huán),Python是一門非常強大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04
  • Python實現(xiàn)K-近鄰算法的示例代碼

    Python實現(xiàn)K-近鄰算法的示例代碼

    k-近鄰算法(K-Nearest Neighbour algorithm),又稱 KNN 算法,是數(shù)據(jù)挖掘技術(shù)中原理最簡單的算法。本文將介紹實現(xiàn)K-近鄰算法的示例代碼,需要的可以參考一下
    2022-09-09

最新評論

亳州市| 黄陵县| 得荣县| 贡山| 本溪市| 慈溪市| 漳平市| 柘城县| 工布江达县| 黄浦区| 伊金霍洛旗| 肇州县| 论坛| 华阴市| 沧源| 皮山县| 左云县| 铅山县| 突泉县| 通化县| 永平县| 凭祥市| 黑水县| 长岭县| 松潘县| 紫阳县| 珲春市| 汉中市| 淳安县| 汉源县| 武川县| 治多县| 灌云县| 贵南县| 潮安县| 茌平县| 苏尼特右旗| 泸州市| 旅游| 扬州市| 东安县|