difflib模块
该模块对字符串文件等的差异性对比,并提供HtmlDiff类来增加阅读性。
字符串比较
#!/usr/bin/env python
#author: mark
#descri:对比text1和text2的内容差别
import difflib
text1 = '''text1:
woshi shui
wobushini
hade'''
text1_lines = text1.split('
')
text2 = '''text2:
woshi shui
wobushini
haodi
haha'''
text2_lines = text2.splitlines()
d = difflib.HtmlDiff()
print(d.make_file(text1_lines, text2_lines))
最后terminal执行 python xx.py >diff.html
结果如下:
nginx配置文件对比脚本:
#!/usr/bin/env python
#author: mark
#descri:nginx配置文件
import difflib, sys
print('--------------注意:请将文件放入到和%s一致的目录或使用绝对路径比较----------------'%sys.argv[0])
try:
file1_name = sys.argv[1]
file2_name = sys.argv[2]
except Exception as e:
print('Erorr:%s'%e)
print('Usage:python %s file1_name file2name'%sys.argv[0])
sys.exit()
def readfile(filename):
try:
with open(filename, 'rb') as file_obj:
contents = ''
while True:
content = file_obj.read(60).decode(encoding='utf-8') #decode解码:bytes to str.
if not content:
break
contents += content
file_lines = contents.splitlines()
return file_lines
except Exception as e:
print('Erorr:%s'%e)
def main():
text1_file = readfile(file1_name)
text2_file = readfile(file2_name)
d = difflib.HtmlDiff()
print(d.make_file(text1_file, text2_file))
if __name__ == '__main__':
main()
运行命令:python diff_nginx.py nginx1.txt nginx2.txt > diff_nginx.html
结果如下:
文件与目录比较
>>> filecmp.cmp("/root/testFileDir/file1", "/root/testFileDir/file2") #单文件比较,默认shallow为True,shallow-->os.stat
True
>>> filecmp.cmp("/root/testFileDir/file1", "/root/testFileDir/file2", shallow=False) #会比较文件内容
True
>>> a = filecmp.cmpfiles('/root/testFileDir/dir1', '/root/testFileDir/dir2', ['file1','file2','file3','file4']) #多文件比较,返回元祖([匹配:两目录文件内容相同], [不匹配:匹配反之], [错误:文件不存在])
>>> '匹配成功文件:%s; 匹配失败文件:%s; 错误文件:%s'%(a[0], a[1], a[2])
"匹配成功文件:['file1']; 匹配失败文件:['file2']; 错误文件:['file3', 'file4']"
>>> a = "/root/testFileDir/dir1"
>>> b = "/root/testFileDir/dir2"
>>> dirobj = filecmp.dircmp(a, b, ['test.py']) #忽略test.py
>>> dirobj.report() #目录的内容
diff /root/testFileDir/dir1 /root/testFileDir/dir2
Only in /root/testFileDir/dir2 : ['b']
Identical files : ['file1']
Differing files : ['file2']
Common subdirectories : ['a']
>>> dirobj.report_partial_closure() #当前目录以及一级子目录
diff /root/testFileDir/dir1 /root/testFileDir/dir2
Only in /root/testFileDir/dir2 : ['b']
Identical files : ['file1']
Differing files : ['file2']
Common subdirectories : ['a']
diff /root/testFileDir/dir1/a /root/testFileDir/dir2/a
Identical files : ['a1', 'a2', 'a3']
>>> dirobj.report_full_closure() #当前目录以及递归
diff /root/testFileDir/dir1 /root/testFileDir/dir2
Only in /root/testFileDir/dir2 : ['b']
Identical files : ['file1']
Differing files : ['file2']
Common subdirectories : ['a']
diff /root/testFileDir/dir1/a /root/testFileDir/dir2/a
Identical files : ['a1', 'a2', 'a3']
>>> dirobj.left
'/root/testFileDir/dir1'
>>> dirobj.right
'/root/testFileDir/dir2'
>>>
>>> dirobj.common #当前目录共同的文件和目录
['a', 'file1', 'file2']
>>> dirobj.common_dirs
['a']
>>> dirobj.common_files
['file1', 'file2']
>>> dirobj.common_funny
[]
>>> dirobj.same_files #当前目录的匹配的文件
['file1']
>>> dirobj.diff_files
['file2']
>>> dirobj.funny_files #都存在但无法比较
[]
案例脚本