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 相关文章推荐
一则python3的简单爬虫代码
May 26 Python
详解Python的Django框架中manage命令的使用与扩展
Apr 11 Python
Mac中升级Python2.7到Python3.5步骤详解
Apr 27 Python
Python2随机数列生成器简单实例
Sep 04 Python
Python操作Sql Server 2008数据库的方法详解
May 17 Python
Python continue继续循环用法总结
Jun 10 Python
基于pip install django失败时的解决方法
Jun 12 Python
pip 安装库比较慢的解决方法(国内镜像)
Oct 06 Python
python使用python-pptx删除ppt某页实例
Feb 14 Python
Selenium alert 弹窗处理的示例代码
Aug 06 Python
Python configparser模块应用过程解析
Aug 14 Python
Python 读取千万级数据自动写入 MySQL 数据库
Jun 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
PHP删除HTMl标签的三种解决方法
2013/06/30 PHP
PHP伪静态Rewrite设置之APACHE篇
2014/07/30 PHP
在php的yii2框架中整合hbase库的方法
2018/09/20 PHP
PHP封装的分页类与简单用法示例
2019/02/25 PHP
Jquery Ajax学习实例5 向WebService发出请求,返回泛型集合数据的异步调用
2010/03/17 Javascript
JSChart轻量级图形报表工具(内置函数中文参考)
2010/10/11 Javascript
js 函数的副作用分析
2011/08/23 Javascript
javascript 进阶篇2 CSS XML学习
2012/03/14 Javascript
JavaScript数据类型判定的总结笔记
2015/07/31 Javascript
jQuery遮罩层实现方法实例详解(附遮罩层插件)
2015/12/08 Javascript
jqueryMobile使用示例分享
2016/01/12 Javascript
JavaScript实现瀑布流布局
2020/06/28 Javascript
JS hashMap实例详解
2016/05/26 Javascript
详解AngularJS如何实现跨域请求
2016/08/22 Javascript
jQuery ajax MD5实现用户注册即时验证功能
2016/10/11 Javascript
AngularJS中指令的四种基本形式实例分析
2016/11/22 Javascript
jquery滚动条插件slimScroll使用方法
2017/02/09 Javascript
PHP7新特性简述
2017/06/11 Javascript
JS利用正则表达式实现简单的密码强弱判断实例
2017/06/16 Javascript
基于JSONP原理解析(推荐)
2017/12/04 Javascript
JavaScript判断变量名是否存在数组中的实例
2017/12/28 Javascript
Vue组件之自定义事件的功能图解
2018/02/01 Javascript
Vue绑定内联样式问题
2018/10/17 Javascript
微信小程序 setData 对 data数据影响问题
2019/04/18 Javascript
微信小程序webSocket的使用方法
2020/02/20 Javascript
vue+element实现图片上传及裁剪功能
2020/06/29 Javascript
H5+css3+js搭建带验证码的登录页面
2020/10/11 Javascript
python实现DNS正向查询、反向查询的例子
2014/04/25 Python
django在接受post请求时显示403forbidden实例解析
2018/01/25 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
pytorch 多分类问题,计算百分比操作
2020/07/09 Python
介绍一下linux的文件系统
2012/03/20 面试题
注塑工厂厂长岗位职责
2013/12/02 职场文书
优秀中学生事迹材料
2014/01/31 职场文书
简历自荐信范文
2015/03/09 职场文书
裁员通知
2015/04/25 职场文书