python filecmp.dircmp实现递归比对两个目录的方法
作者:longfeiwlf 时间:2021-10-11 10:22:29
使用python filecmp模块的dircmp类可以很方便的比对两个目录,dircmp的用法已经有很多文章介绍,不再赘述。
可以help(filecmp.dircmp)查看帮助信息,其中提到的x.report()、x.report_partial_closure()
,都只能打印两目录一级子目录的比较信息。而x.report_full_closure()
可以递归打印所有子目录的比对信息,但是输出太多,大多数情况下我们可能只关心两目录的不同之处。
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.
本文编写的脚本,重点关注并实现两个目标:
1)递归比对两个目录及其所有子目录。
2)仅输出两目录不同之处,包括文件名相同(common_files)但是文件不一致(diff_files),以及左、右目录中独有的文件或子目录。
py脚本compare_dir.py内容如下:
# -*- coding: utf-8 -*-
"""
@desc 使用filecmp.dircmp递归比对两个目录,输出比对结果以及统计信息。
@author longfeiwlf
@date 2020-5-20
"""
from filecmp import dircmp
import sys
# 定义全局变量:
number_different_files = 0 # 文件名相同但不一致的文件数
number_left_only = 0 # 左边目录独有的文件或目录数
number_right_only = 0 # 右边目录独有的文件或目录数
def print_diff(dcmp):
"""递归比对两目录,如果有不同之处,打印出来,同时累加统计计数。"""
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比对结果详情: ")
print_diff(mydcmp)
if (number_different_files == 0 and number_left_only == 0
and number_right_only == 0):
print("\n两个目录完全一致!")
else:
print("\n比对结果统计:")
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脚本使用举例:
来源:https://blog.csdn.net/longfeizzu/article/details/106233479
标签:filecmp.dircmp,python,递归
0
投稿
猜你喜欢
从XML中读取数据到内存的实例
2008-09-04 14:43:00
PHP手机号码归属地查询代码(API接口/mysql)
2023-10-26 04:35:44
使用python开发vim插件及心得分享
2023-11-22 11:30:32
ASP.NET 2.0中的数据操作之九:跨页面的主/从报表
2023-07-19 20:28:42
python实现水印图片功能
2021-07-04 00:45:45
朴素贝叶斯算法的python实现方法
2023-03-01 07:05:53
Python入门教程(三十九)Python的NumPy安装与入门
2023-08-25 10:38:30
浅谈flask源码之请求过程
2023-12-17 10:36:48
网页屏蔽鼠标左右键和键盘按键功能
2007-10-17 21:30:00
pytorch model.cuda()花费时间很长的解决
2021-12-08 08:02:06
PHP getDocNamespaces()函数讲解
2023-06-13 22:19:06
聊天室php&mysql(五)
2023-11-19 20:18:22
CTF中的PHP特性函数解析之中篇
2023-06-11 12:56:20
Python 私有属性和私有方法应用场景分析
2023-12-06 05:36:42
8种常用的Python工具
2023-03-28 10:49:49
python绘制规则网络图形实例
2021-08-31 02:28:28
python爬取招聘要求等信息实例
2021-01-27 21:22:36
python2.x实现人民币转大写人民币
2023-06-26 10:35:53
何处安放的Loading
2011-08-10 19:11:20
Perl下应当如何连接Access数据库
2008-12-04 13:06:00