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

1 Answer

+2 votes
by (75.1k points)
selected by
Best answer

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")


    print("Files are not same")