Python | filecmp.cmpfiles() method

19 Mar, 2022
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) Parameter: 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.  
For Example: 
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. 
Python3
# Python program to explain filecmp.cmpfiles() method   # importing filecmp moduleimport filecmp # Path of first directorydir1 = "/home / User / Documents" # Path of second directorydir2 = "/home / User / Desktop"  # Common filescommon = ["file1.txt", "file2.txt", "file3.txt"] # Shallow compare the files# common in both directories match, mismatch, errors = filecmp.cmpfiles(dir1, dir2, common) # Print the result of# shallow comparisonprint("Shallow comparison:")print("Match :", match)print("Mismatch :", mismatch)print("Errors :", errors, "\n")  # Compare the# contents of both files# (i.e deep comparison)match, mismatch, errors = filecmp.cmpfiles(dir1, dir2, common,                                              shallow = False) # Print the result of# deep comparisonprint("Deep comparison:")print("Match :", match)print("Mismatch :", mismatch)print("Errors :", errors)
Shallow comparison:
Match : ['file1.txt', 'file2.txt', 'file3.txt']
Mismatch : []
Errors : []  

Deep comparison:
Match : ['file1.txt', 'file2.txt']
Mismatch : ['file3.txt']
Errors : []
 
thechosenone
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python