python filecmp.dircmp實(shí)現(xiàn)遞歸比對(duì)兩個(gè)目錄的方法
使用python filecmp模塊的dircmp類可以很方便的比對(duì)兩個(gè)目錄,dircmp的用法已經(jīng)有很多文章介紹,不再贅述。
可以help(filecmp.dircmp)查看幫助信息,其中提到的x.report()、x.report_partial_closure()
,都只能打印兩目錄一級(jí)子目錄的比較信息。而x.report_full_closure()
可以遞歸打印所有子目錄的比對(duì)信息,但是輸出太多,大多數(shù)情況下我們可能只關(guān)心兩目錄的不同之處。
help(filecmp.dircmp) 摘選: | High level usage: | x = dircmp(dir1, dir2) | x.report() -> prints a report on the differences between dir1 and dir2 | or | x.report_partial_closure() -> prints report on differences between dir1 | and dir2, and reports on common immediate subdirectories. | x.report_full_closure() -> like report_partial_closure, | but fully recursive.
本文編寫的腳本,重點(diǎn)關(guān)注并實(shí)現(xiàn)兩個(gè)目標(biāo):
1)遞歸比對(duì)兩個(gè)目錄及其所有子目錄。
2)僅輸出兩目錄不同之處,包括文件名相同(common_files)但是文件不一致(diff_files),以及左、右目錄中獨(dú)有的文件或子目錄。
py腳本compare_dir.py內(nèi)容如下:
# -*- coding: utf-8 -*- """ @desc 使用filecmp.dircmp遞歸比對(duì)兩個(gè)目錄,輸出比對(duì)結(jié)果以及統(tǒng)計(jì)信息。 @author longfeiwlf @date 2020-5-20 """ from filecmp import dircmp import sys # 定義全局變量: number_different_files = 0 # 文件名相同但不一致的文件數(shù) number_left_only = 0 # 左邊目錄獨(dú)有的文件或目錄數(shù) number_right_only = 0 # 右邊目錄獨(dú)有的文件或目錄數(shù) def print_diff(dcmp): """遞歸比對(duì)兩目錄,如果有不同之處,打印出來,同時(shí)累加統(tǒng)計(jì)計(jì)數(shù)。""" global number_different_files global number_left_only global number_right_only for name in dcmp.diff_files: print("diff_file found: %s/%s" % (dcmp.left, name)) number_different_files += 1 for name_left in dcmp.left_only: print("left_only found: %s/%s" % (dcmp.left, name_left)) number_left_only += 1 for name_right in dcmp.right_only: print("right_only found: %s/%s" % (dcmp.right, name_right)) number_right_only += 1 for sub_dcmp in dcmp.subdirs.values(): print_diff(sub_dcmp) # 遞歸比較子目錄 if __name__ == '__main__': try: mydcmp = dircmp(sys.argv[1], sys.argv[2]) except IndexError as ie: print(ie) print("使用方法:python compare_dir_cn.py 目錄1 目錄2") else: print("\n比對(duì)結(jié)果詳情: ") print_diff(mydcmp) if (number_different_files == 0 and number_left_only == 0 and number_right_only == 0): print("\n兩個(gè)目錄完全一致!") else: print("\n比對(duì)結(jié)果統(tǒng)計(jì):") print("Total Number of different files is: " + str(number_different_files)) print("Total Number of files or directories only in '" + sys.argv[1] + "' is: " + str(number_left_only)) print("Total Number of files or directories only in '" + sys.argv[2] + "' is: " + str(number_right_only))
compare_dir.py腳本使用舉例:
總結(jié)
到此這篇關(guān)于filecmp.dircmp實(shí)現(xiàn)遞歸比對(duì)兩個(gè)目錄的文章就介紹到這了,更多相關(guān)filecmp.dircmp實(shí)現(xiàn)遞歸比對(duì)兩個(gè)目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python如何實(shí)現(xiàn)遞歸轉(zhuǎn)非遞歸
- 10張動(dòng)圖學(xué)會(huì)python循環(huán)與遞歸問題
- Python非單向遞歸函數(shù)如何返回全部結(jié)果
- python遞歸函數(shù)用法詳解
- Python尾遞歸優(yōu)化實(shí)現(xiàn)代碼及原理詳解
- python如何停止遞歸
- python利用遞歸方法實(shí)現(xiàn)求集合的冪集
- Python函數(shù)遞歸調(diào)用實(shí)現(xiàn)原理實(shí)例解析
- python實(shí)現(xiàn)文法左遞歸的消除方法
- python遞歸函數(shù)求n的階乘,優(yōu)缺點(diǎn)及遞歸次數(shù)設(shè)置方式
- python 遞歸相關(guān)知識(shí)總結(jié)
相關(guān)文章
Python利用神經(jīng)網(wǎng)絡(luò)解決非線性回歸問題實(shí)例詳解
這篇文章主要介紹了Python利用神經(jīng)網(wǎng)絡(luò)解決非線性回歸問題,結(jié)合實(shí)例形式詳細(xì)分析了Python使用神經(jīng)網(wǎng)絡(luò)解決非線性回歸問題的相關(guān)原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07Python直接使用plot()函數(shù)畫圖的方法實(shí)例
Python非常簡(jiǎn)單而又非常強(qiáng)大,它的功能之一就是畫出漂亮的圖表,實(shí)現(xiàn)數(shù)據(jù)的可視化,下面這篇文章主要給大家介紹了關(guān)于Python直接使用plot()函數(shù)畫圖的相關(guān)資料,需要的朋友可以參考下2022-05-05Django使用詳解:ORM 的反向查找(related_name)
今天小編就為大家分享一篇Django使用詳解:ORM 的反向查找(related_name),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05利用Python實(shí)現(xiàn)文件讀取與輸入以及數(shù)據(jù)存儲(chǔ)與讀取的常用命令
這篇文章主要給大家介紹了關(guān)于利用Python實(shí)現(xiàn)文件讀取與輸入以及數(shù)據(jù)存儲(chǔ)與讀取的常用命令,文中還介紹了用python循環(huán)保存文件并循環(huán)讀取文件的方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11