python监控difflib模块,对比文件内容差异。
1、两个字符串的差异对比
生成美观的对比html格式文件:
运行:
2、对比nginx配置文件差异
#!/usr/bin/env python
import difflib
import sys
try:
textfile1 = sys.argv[1]
textfile2 = sys.argv[2]
except Exception,e:
print "Error:" + str(e)
print "Usage: test.py filename1 filename2"
sys.exit()
def readfile(filename):
try:
fileHandle = open(filename,'rb')
text = fileHandle.read().splitlines()
fileHandle.close()
return text
except IOError as error:
print ('Read file Error:' + str(error))
sys.exit()
if textfile1 == "" or textfile2 == "":
print "Usage:test.py filename1 filename2"
sys.exit()
text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)