脚本:
#!/usr/bin/python
import difflib
import sys
from python.auto.yunwei.diff1 import text1_lines
try:
textfile1=sys.argv[1]
textfile2=sys.argv[2]
except Exception,e:
print "Error:"+str(e)
print "Usage: diff3.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:diffe.py filename1 filename2"
sys.exit()
text1_lines=readfile(textfile1)
text2_lines=readfile(textfile2)
d=difflib.HtmlDiff()
print d.make_file(text1_lines, text2_lines)
第一种方法
import difflib
text1='''text1:
This module provides
defflib document v7.4
add string
'''
text1_lines=text1.splitlines()
text2='''
this module provedess
defflib document v7.5
'''
text2_lines=text2.splitlines()
d=difflib.Differ()
deff=d.compare(text1_lines, text2_lines)
print '
'.join(list(deff))
第二种方法:用浏览器阅读
import difflib
text1='''text1:
This module provides
defflib document v7.4
add string
'''
text1_lines=text1.splitlines()
from difflib import HtmlDiff
text2='''
this module provedess
defflib document v7.5
'''
text2_lines=text2.splitlines()
d=HtmlDiff()
diffs=d.make_file(text1_lines,text2_lines)
print diffs