XD blog

blog page

compare


2014-09-21 How to compare two text files?

Many tools can be used to compare the differences between two files: Comparison of file comparison tools, Top 5 diff tools. Python has a module fsdifflib but this is not very visual. Another option is to use javascript and a tool such as jsdifflib.

I made a function which writes a HTML page with the javascript code which displays the diff between two files.

from pysquickhelper.sync.visual_sync import create_visual_diff_through_html
with open("file1.txt","r",encoding="utf8") as f : text1 = f.read()
with open("file2.txt","r",encoding="utf8") as f : text2 = f.read()
pg = create_visual_diff_through_html(text1,text2)
with open("page.html","w",encoding="utf8") as f : f.write(pg)
import webbrowser
webbrowser.open("page.html")

Xavier Dupré