- Using difflib library:
Python has a Module which is specially used for comparing the differences between the files. To get differences using the difflib library, we have to call the unified_diff() function to this comparison.
There is one Class available for comparing the differences between the files which named as Differ inside the difflib library.
You can try any of the above I guess both works fine, just import the library first like from difflib import Differ
from difflib import Differ
with open('file1.txt') as file_1, open('file2.txt') as file_2:
differ = Differ()
for line in differ.compare(file_1.readlines(), file_2.readlines()):
print(line)