In this post, we will take a look at how to compare two files using Python.
I was
tasked to compare 2 files and then list the differences between them using
Python.
Initially,
I started with filecmp module, but even with the function parameter
‘shallow’ set to false, the Boolean result was not enough.
Sure, it
can act as an indicator to take some action, but it will not list the differences.
I was
looking for something more visual, something like color coding and not like the
git diff output, which is not very user-friendly.
But, another
Python internal module, difflib helped me to get the job done.
Inside
Difflib, HtmlDiff is what I was looking for.
The
differences were highlighted with 3 different colors and also the line numbers
were indicated in a table to locate the differences.
The results
are quite self-explanatory and it is easier to explain the differences to other
people.
Code for generating the above difference table:
Note: File1.txt and File2.txt should be created in the same directory as the script before running the code.
My Opinion
HtmlDiff
was the best solution for me. Also, I had a template that I used for
generating the difference report. The difference tables result from the HtmlDif.make_file
output was extracted and added to the template report using BeautifulSoup.
Multiple
other parameters of the HtmlDiff class can be changed to control the table
headers, columnwidth, etc. Do check documentation
for more details.
Thank You!!
Comments
Post a Comment