python开发文本文件对比


 1 #coding:utf-8
 2 #!/usr/bin/python
 3 from difflib import *
 4 import sys
 5 import os
 6 reload(sys)
 7 sys.setdefaultencoding( "utf-8" )
 8 from Tkinter import *
 9 import tkFileDialog;
10 import tkMessageBox;
11 
12 root=Tk();
13 root.geometry("280x300+983+630");
14 root.attributes("-alpha",1)
15 root.attributes("-topmost",1);
16 root.title=("Contrast");
17 
18 filename2 = tkFileDialog.askopenfilename(initialdir = 'C:\Python');
19 label1=Label(root,text=filename2); 
20 label1.pack();
21 
22 filename4 = tkFileDialog.askopenfilename(initialdir = 'C:\Python');
23 label2=Label(root,text=filename4); 
24 label2.pack();
25 
26 def diffcommand(filename):
27     fileHandle = open(filename,"rb")
28     text=fileHandle.read().splitlines()
29     fileHandle.close()
30     return text
31 
32 def diffbtn():
33     if filename2=="" or filename4=="":
34         tkMessageBox.showinfo("Sorry","Please select file")
35     else:
36         text1_lines=diffcommand(filename2)
37         text2_lines=diffcommand(filename4)
38         s=HtmlDiff.make_file(HtmlDiff(),text1_lines,text2_lines)
39         f=open(r"c:\对比结果.html",'w')
40         f.write(s)
41         f.close()
42 
43 Button(root,text="Contrast",state=ACTIVE,bg="blue",command=diffbtn).pack(side=LEFT)
44 root.mainloop()
thinter