Skip to content
Related Articles

Related Articles

Improve Article

Python | filecmp.cmpfiles() method

  • Last Updated : 01 Jan, 2020

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, 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.




# Python program to explain filecmp.cmpfiles() method 
    
# importing filecmp module 
import filecmp
  
# Path of first directory
dir1 = "/home / User / Documents"
  
# Path of second directory
dir2 = "/home / User / Desktop"
   
# Common files
common = ["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 comparison
print("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 comparison
print("Deep comparison:")
print("Match :", match)
print("Mismatch :", mismatch)
print("Errors :", errors)
Output:
Shallow comparison:
Match : ['file1.txt', 'file2.txt', 'file3.txt']
Mismatch : []
Errors : []  

Deep comparison:
Match : ['file1.txt', 'file2.txt']
Mismatch : ['file3.txt']
Errors : []

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :