­

python使用difflib对比文件示

使用difflib模块对比文件内容

1  示例:字符串差异对比 

vim duibi.py

#!/usr/bin/env python  # -*- coding: utf-8 -*-  import re  import os  import difflib  tex1="""tex1:  this is a test for difflib ,just try to get difference of the log  现在试试功能是否可行 好呀  goodtest  那么试试吧好人  """  tex1_lines=tex1.splitlines()  tex2="""tex2:  this is a test for difflib ,just try to get difference of the log  现在试试功能是否可行  goodtast  那么试试吧  """  tex2_lines=tex2.splitlines()  #---------原始对比方法----------  #d=difflib.Differ()  #diff=d.compare(tex1_lines,tex2_lines)  #print 'n'.join(list(diff))    #--------html对比方法----------  #并修改diff.html的编码,将ISO-8859-1改为UTF-8格式解析文件,用于对比中文  d=difflib.HtmlDiff()  q=d.make_file(tex1_lines,tex2_lines)  old_str='charset=ISO-8859-1'  new_str='charset=UTF-8'  with open('diff.html','w') as f_new:  	f_new.write(q.replace(old_str,new_str))    #############################  #d=difflib.HtmlDiff()  #q=d.make_file(tex1_lines,tex2_lines)  #old_str='charset=ISO-8859-1'  #new_str='charset=UTF-8'  #data=q.replace(old_str,new_str)  #fo=open('diff.html','w')  #fo.write(data)  #fo.close()  ############################

运行 python duibi.py  生产diff.html

浏览器打开diff.html  查看对比结果。

2 示例 文件对比  文件差异对比代码 可直接使用 无需修改(包括中文)

用下面脚本对比  testfile1 testfile2 的差异   

vim diff.py

#!/usr/bin/env python  # -*- coding: utf-8 -*-  import os  import sys  import difflib    try:  	tfile1=sys.argv[1]  	tfile2=sys.argv[2]  except Exception,e:  	print "错误:"+str(e)  	print "请准确输入参数,例如:python diff.py file1 file2"  	sys.exit()  def readfile(filename):  	try:  		fileHandle=open(filename,'rb')  		lines=fileHandle.read().splitlines()  		fileHandle.close()  		return lines  	except IOError as error:  		print('读取文件错误:'+str(error))  		sys.exit()  if tfile1=="" or tfile2=="":  	print "请准确输入参数,例如:python diff.py file1 file2"  	sys.exit()    tfile1_lines=readfile(tfile1)  tfile2_lines=readfile(tfile2)    #d=difflib.HtmlDiff()  #print s.make_file(tfile1_lines,tfile2_lines)    #为了生成html能识别中文,可用下面代码 #修改diff.html的编码,将ISO-8859-1改为UTF-8  #====================================  #方法1:  #d=difflib.HtmlDiff()  #q=d.make_file(tfile1_lines,tfile2_lines)  #old_str='charset=ISO-8859-1'  #new_str='charset=UTF-8'  #data=q.replace(old_str,new_str)  #fo=open('diff.html','w')  #fo.write(data)  #fo.close()  #====================================  #方法2:  #d=difflib.HtmlDiff()  #q=d.make_file(tfile1_lines,tfile2_lines)  #old_str='charset=ISO-8859-1'  #new_str='charset=UTF-8'  #fo=open('diff.html','w')  #fo.write(q)  #fo.close()  #with open('diff.html','r') as f:  #        lines=f.readlines()  #with open('diff.html','w') as f_new:  #        for line in lines:  #                f_new.write(line.replace(old_str,new_str))  #=====================================  #方法3:  old_str='charset=ISO-8859-1'  new_str='charset=UTF-8'  d=difflib.HtmlDiff()  q=d.make_file(tfile1_lines,tfile2_lines)  with open('diff.html','w') as f_new:  	f_new.write(q.replace(old_str,new_str))

执行python diff.py testfile1 testfile2    

生成diff.html

浏览器查看文件对比结果