python filecmp.dircmp实现递归比对两个目录的方法


Posted in Python onMay 22, 2020

使用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脚本使用举例:

python filecmp.dircmp实现递归比对两个目录的方法

总结

到此这篇关于filecmp.dircmp实现递归比对两个目录的文章就介绍到这了,更多相关filecmp.dircmp实现递归比对两个目录内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
浅析Python中MySQLdb的事务处理功能
Sep 21 Python
Python 判断是否为质数或素数的实例
Oct 30 Python
Python装饰器用法实例分析
Jan 14 Python
python中嵌套函数的实操步骤
Feb 27 Python
pandas DataFrame 行列索引及值的获取的方法
Jul 02 Python
Python使用matplotlib实现交换式图形显示功能示例
Sep 06 Python
Python3 虚拟开发环境搭建过程(图文详解)
Jan 06 Python
Macbook安装Python最新版本、GUI开发环境、图像处理、视频处理环境详解
Feb 17 Python
Python3 xml.etree.ElementTree支持的XPath语法详解
Mar 06 Python
Python代码一键转Jar包及Java调用Python新姿势
Mar 10 Python
关于python爬虫应用urllib库作用分析
Sep 04 Python
python实现简单的三子棋游戏
Apr 28 Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
May 22 #Python
Python参数传递对象的引用原理解析
May 22 #Python
Python configparser模块常用方法解析
May 22 #Python
keras中的卷积层&池化层的用法
May 22 #Python
Keras Convolution1D与Convolution2D区别说明
May 22 #Python
Python pip安装模块提示错误解决方案
May 22 #Python
keras中的backend.clip用法
May 22 #Python
You might like
第五节 克隆 [5]
2006/10/09 PHP
PHP 命令行参数详解及应用
2011/05/18 PHP
php操作JSON格式数据的实现代码
2011/12/24 PHP
初识Laravel
2014/10/30 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
2016/10/10 PHP
详解PHP如何更好的利用PHPstorm的自动提示
2017/08/18 PHP
JavaScript栏目列表隐藏/显示简单实现
2013/04/03 Javascript
node.js中的console.timeEnd方法使用说明
2014/12/09 Javascript
jQuery晃动层特效实现方法
2015/03/09 Javascript
jQuery插件multiScroll实现全屏鼠标滚动切换页面特效
2015/04/12 Javascript
jQuery获取URL请求参数的方法
2015/07/18 Javascript
js实现不重复导入的方法
2016/03/02 Javascript
20分钟成功编写bootstrap响应式页面 就这么简单
2016/05/12 Javascript
javascript验证内容为数字以及长度为10的简单实例
2016/08/20 Javascript
bootstrap读书笔记之CSS组件(上)
2016/10/17 Javascript
JS实现深度优先搜索求解两点间最短路径
2019/01/17 Javascript
浅谈JavaScript_DOM学习篇_图片切换小案例
2019/03/19 Javascript
微信公众号获取用户地理位置并列出附近的门店的示例代码
2019/07/25 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
vue+canvas实现拼图小游戏
2020/09/18 Javascript
python中循环语句while用法实例
2015/05/16 Python
python读取中文txt文本的方法
2018/04/12 Python
Python设计模式之桥接模式原理与用法实例分析
2019/01/10 Python
在Python中合并字典模块ChainMap的隐藏坑【推荐】
2019/06/27 Python
Python3批量移动指定文件到指定文件夹方法示例
2019/09/02 Python
python基于三阶贝塞尔曲线的数据平滑算法
2019/12/27 Python
python中rb含义理解
2020/06/18 Python
Pygame框架实现飞机大战
2020/08/07 Python
Dr. Martens马汀博士澳大利亚官网:马丁靴鼻祖
2019/07/02 全球购物
最新大学毕业求职简历的自我评价
2013/10/18 职场文书
运动会通讯稿50字
2014/01/30 职场文书
学校安全责任书
2014/04/14 职场文书
团委书记的竞聘演讲稿
2014/04/24 职场文书
元旦联欢会策划方案
2014/06/11 职场文书
2017年寒假社区服务活动总结
2016/04/06 职场文书
从贫穷到富有,是知识技能和学习力的差别
2019/08/20 职场文书