使用Python自带difflib模块进行文件内容差异对比

difflib_text.py

#!/usr/bin/python
import difflib
import sys

try:
    textfile1=sys.argv[1]
    textfile2=sys.argv[2]
except Exception,e:
    print "Error:"+str(e)
    print "Usage: difflib_text.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: simple3.py filename1 filename2"
    sys.exit()

text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)

d = difflib.HtmlDiff()
print d.make_file(text1_lines,text2_lines)

测试:python difflib_text.py nginxdiff.conf nginxdiff2.conf

结果:

 

搜索

posted @ 2018-03-24 09:45  人艰不拆_zmc  阅读(552)  评论(0编辑  收藏  举报