Python - comparing two text files with difflib.HtmlDiff()


Python tip:

You can use HtmlDiff to generate an HTML table that shows a side by side, line by line comparison of the text with inter-line and intra-line change highlights.

https://docs.python.org/3/library/difflib.html#difflib.HtmlDiff

An example👇

import difflib
from pathlib import Path

first_file_lines = Path('first.txt').read_text().splitlines()
second_file_lines = Path('second.txt').read_text().splitlines()

html_diff = difflib.HtmlDiff().make_file(first_file_lines, second_file_lines)
Path('diff.html').write_text(html_diff)