+3 votes
in Programming Languages by (48.9k points)
I want to compare two files using Python code. Which Python function should I use?

1 Answer

+2 votes
by (63.5k points)

The cmp() function of the filecmp module can be used to compare two files. It compares the files and returns True if they seem equal, and False otherwise.

filecmp.cmp(f1, f2, shallow=True)

Here is an example:

from filecmp import cmp

if cmp(file1 file2):

    print("Files are same")

else:

    print("Files are not same")


...