使用Python进行目录的对比
来源:互联网 发布:shift 在js中 编辑:程序博客网 时间:2024/06/16 21:13
如果进行单个文件的比较,可以使用difflib模块。虽然filecmp模块也能够进行单个文件的对比,但是前者能够提供观感更好的报告。如果我们只是想看一下两个目录中的某个文件是否一致而不关系其中的内容,那么后者或许是更改选的选择。
如果进行目录的对比,那么可以使用filecmp模块了。在IPython中进行简单的命令交互演示功能如下:
In [1]:import filecmp
In [2]:ls
Chapter_01/Chapter_02/
In [3]:dirobj = filecmp.dircmp('Chapter_01','Chapter_02')
输出两个目录差异报告
In [4]:dirobj.report()
diff Chapter_01 Chapter_02
Only in Chapter_01 : ['ip.py', 'os_mem.py', 'pid.py']
Only in Chapter_02 : ['d1.py', 'd2.py', 'diff.py', 'diff.zip', 'dns_parser.py', 'join.py', 'pydiff.py', 'report.html']
Identical files : ['c01.py']
输出部分报告(最外面的目录曾经,这里与第一个无差异是因为没有子目录)
In [5]:dirobj.report_partial_closure()
diff Chapter_01 Chapter_02
Only in Chapter_01 : ['ip.py', 'os_mem.py', 'pid.py']
Only in Chapter_02 : ['d1.py', 'd2.py', 'diff.py', 'diff.zip', 'dns_parser.py', 'join.py', 'pydiff.py', 'report.html']
Identical files : ['c01.py']
输出全部报告
In [6]:dirobj.report_full_closure()
diff Chapter_01 Chapter_02
Only in Chapter_01 : ['ip.py', 'os_mem.py', 'pid.py']
Only in Chapter_02 : ['d1.py', 'd2.py', 'diff.py', 'diff.zip', 'dns_parser.py', 'join.py', 'pydiff.py', 'report.html']
Identical files : ['c01.py']
查看一下输出结果对象格式,这个格式略显奇怪了一些
In [7]: type(dirobj.report_full_closure())
diff Chapter_01 Chapter_02
Only in Chapter_01 : ['ip.py', 'os_mem.py', 'pid.py']
Only in Chapter_02 : ['d1.py', 'd2.py', 'diff.py', 'diff.zip', 'dns_parser.py', 'join.py', 'pydiff.py', 'report.html']
Identical files : ['c01.py']
Out[7]:NoneType
尝试把结果对象转换为字符串
In [8]: str(dirobj.report_full_closure())
diff Chapter_01 Chapter_02
Only in Chapter_01 : ['ip.py', 'os_mem.py', 'pid.py']
Only in Chapter_02 : ['d1.py', 'd2.py', 'diff.py', 'diff.zip', 'dns_parser.py', 'join.py', 'pydiff.py', 'report.html']
Identical files : ['c01.py']
Out[8]:'None'
输出左边目录的清单
In [9]:dirobj.left_list
Out[9]:['c01.py', 'ip.py', 'os_mem.py', 'pid.py']
右边目录清单
In [10]:dirobj.right_list
Out[10]:
['c01.py',
'd1.py',
'd2.py',
'diff.py',
'diff.zip',
'dns_parser.py',
'join.py',
'pydiff.py',
'report.html']
仅在右边目录中存在的文件清单
In [11]:dirobj.right_only
Out[11]:
['d1.py',
'd2.py',
'diff.py',
'diff.zip',
'dns_parser.py',
'join.py',
'pydiff.py',
'report.html']
共有的子目录
In [12]:dirobj.common_dirs
Out[12]:[]
共有的文件
In [13]:dirobj.common_files
Out[13]:['c01.py']
无法比较的目录
In [14]:dirobj.common_funny
Out[14]:[]
相同的文件
In [15]:dirobj.same_files
Out[15]:['c01.py']
无法比较的文件
In [16]:dirobj.funny_files
Out[16]:[]
在MATLAB中有一个我经常使用的命令——visdiff,可以用于文件或者目录的对比。如此比较下来,Python基本上也提供了MATLAB相应命令所提供的功能。不过,MATLAB的使用更为简单,似乎也更为细致一些。不过,考虑使用Python还是有必要的。首先是Python免费,其次是启动速度大概比MATLAB快不少吧!除此之外,现在的Python凑代码也是蛮简单的,在自己需要的时候几行代码拼凑一般就能够实现自己要的功能了。
- 使用Python进行目录的对比
- python简单实现目录对比
- python 进行文本相似性对比
- python进行两个表格对比
- Python——目录对比源代码
- Python中isinstance和type进行对比
- 使用 python 遍历目录下的文件
- python的一个对比
- python小实例--定期检查源目录与备份目录的差异进行备份
- 使用pdb进行python的调试
- 使用python的nose模块进行测试
- 使用Python进行简单的图像处理
- 使用Python进行FME的批处理
- 使用Python进行FME的批处理
- 使用python进行数据的采集
- python使用struct进行二进制文件的读写
- 活动目录对比工具-DSCompare使用小结
- 使用JAVA库进行LDAP目录读取的例子
- lintcode(402)连续子数组求和
- 缓存和内存的区别
- MFC Windows程序设计--公用控件
- Version 1.6.0 of the JVM is not suitable for the this product.Version:1.8 or greater is required
- C语言常用头文件及库函数——string.h
- 使用Python进行目录的对比
- 磁盘管理
- Java中缓存之内存缓存
- Js进阶知识(一)
- 常用排序算法C++版小结
- ACM-动态规划21-三角形最长路径问题
- 微信付款
- [COGS826][Tyvj Feb11] GF打dota(k短路)
- bzoj 2809 左偏树