Python File Comparison Tool Using difflib and Tkinter
This tutorial demonstrates how to build a simple graphical Python application that lets users select two files, compares their contents with the standard difflib module, and generates an HTML report highlighting differences, complete with full source code examples.
The article explains a practical use case where developers need to compare two code files to locate differences, especially when debugging student submissions against reference solutions.
It introduces Python's built‑in difflib module, which can generate side‑by‑side HTML diff reports without any external installation, and shows how to combine it with tkinter to create a small GUI for file selection.
Key steps:
Select the two files to compare using file‑dialog buttons.
Read the files, feed their lines to difflib.HtmlDiff() , and write the resulting HTML to result1.html .
Display the GUI window with labels, entry fields, and a "Compare" button that triggers the diff operation.
Complete source code:
<code>import difflib
import tkinter as tk
import tkinter.filedialog
# 选择文件
def button1():
global file1
file1 = tk.filedialog.askopenfilename()
txt_path1.set(file1)
# 选择文件
def button2():
global file2
file2 = tk.filedialog.askopenfilename()
txt_path2.set(file2)
# 对比文件
def Diff():
with open(file1) as f1, open(file2) as f2:
text1 = f1.readlines()
text2 = f2.readlines()
d = difflib.HtmlDiff()
with open('result1.html', 'w') as f:
f.write(d.make_file(text1, text2))
# 建立主窗口
window = tk.Tk()
window.title('用Python实现文件对比分析')
window.geometry('650x200')
label = tk.Label(window, text='请选择需要对比的文件:', fg='blue', font=('Arial', 12)).place(x=30, y=30)
l1 = tk.Label(window, text='原文件:', font=('Arial', 12)).place(x=30, y=80)
l2 = tk.Label(window, text='目标文件:', font=('Arial', 12)).place(x=30, y=110)
txt_path1 = tk.StringVar()
text1 = tk.Entry(window, textvariable=txt_path1, show=None, width=60)
txt_path2 = tk.StringVar()
text2 = tk.Entry(window, textvariable=txt_path2, show=None, width=60)
text1.place(x=120, y=80)
text2.place(x=120, y=110)
button1 = tk.Button(window, width=8, height=1, text='选择文件', bg='skyblue', command=button1).place(x=550, y=80)
button2 = tk.Button(window, width=8, height=1, text='选择文件', bg='skyblue', command=button2).place(x=550, y=110)
button3 = tk.Button(window, width=20, height=1, text='文件对比', fg='red', bg='orange', command=Diff).place(x=220, y=150)
window.mainloop()
</code>Running the program opens a window where users can click "Select File" buttons to choose the source and target files, then press "File Compare" to produce result1.html . The generated HTML highlights differing lines in red, making visual inspection straightforward.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.