Python | filecmp.cmpfiles() method
Filecmp module in Python provides functions to compare files and directories. This module comes under Python’s standard utility modules. This module also consider the properties of files and directories for comparison in addition to data in them.
filecmp.cmpfiles() method in Python is used to compare files in two directories. Multiple files can be compared using this method. This method returns three lists of file names namely match, mismatch and errors. The match list contains the list of files that matched on comparison, the mismatch list contains the names of those files that don’t, and the errors lists contains the names of files which could not be compared.
This method by default performs shallow comparison (as by default shallow = True) that means only the os.stat() signatures (like size, date modified etc.) of files are compared and if they have identical signatures then files are considered to be equal irrespective of contents of the files. If shallow is set to False then the comparison is done by comparing the contents of the files.
Syntax: filecmp.cmpfiles(dir1, dir2, common, shallow = True)
dir1: The path of first directory. It can be a string, bytes or os.PathLike object representing the path of the directory.
dir2: The path of second directory. It can be a string, bytes or os.PathLike object representing the path of the directory.
common: A list representing the names of the files that will be compared in dir1 and dir2.
shallow (optional): A bool value ‘True’ or ‘False’. The default value of this parameter is True. If its value is True then only the metadata of files are compared. If False then the contents of the files are compared.
Return Type: This method returns a tuple of three lists which represents match, mismatch and errors lists.
filecmp.cmpfiles(dir1, dir2, [file1, file2, fil3]) will compare dir1/file1 with dir2/file1, dir1/file2 with dir2/file2 and dir1/file3 with dir2/file3 and will return match, mismatch and errors list.
Example: Use of filecmp.cmpfiles() method to compare files in two directories.
Shallow comparison: Match : ['file1.txt', 'file2.txt', 'file3.txt'] Mismatch :  Errors :  Deep comparison: Match : ['file1.txt', 'file2.txt'] Mismatch : ['file3.txt'] Errors :