In this Python tutorial, we will learn how to check if two files have the same content. This can be useful for verifying data integrity, detecting duplicate files, or ensuring that a backup file matches the original. We will use two different approaches:
filecmp
module for an optimized comparison.import filecmp
# Method 1: Using filecmp module
def compare_files(file1, file2):
if filecmp.cmp(file1, file2, shallow=False):
print(f"The files '{file1}' and '{file2}' have the same content.")
else:
print(f"The files '{file1}' and '{file2}' have different content.")
# Method 2: Comparing files line by line
def compare_files_line_by_line(file1, file2):
try:
with open(file1, 'r') as f1, open(file2, 'r') as f2:
if f1.readlines() == f2.readlines():
print(f"The files '{file1}' and '{file2}' have the same content.")
else:
print(f"The files '{file1}' and '{file2}' have different content.")
except FileNotFoundError:
print("One or both files do not exist.")
# Specify file names
file1 = "file1.txt"
file2 = "file2.txt"
# Check if files have the same content
compare_files(file1, file2)
compare_files_line_by_line(file1, file2)
file1.txt
and file2.txt
have the same content:The files 'file1.txt' and 'file2.txt' have the same content.
The files 'file1.txt' and 'file2.txt' have the same content.
file1.txt
and file2.txt
have different content:The files 'file1.txt' and 'file2.txt' have different content.
The files 'file1.txt' and 'file2.txt' have different content.
One or both files do not exist.
filecmp.cmp()
(Method 1):
filecmp
module provides a built-in function filecmp.cmp(file1, file2, shallow=False)
.shallow=False
ensures that the comparison is based on actual file content and not just metadata.readlines()
.FileNotFoundError
is caught, and an appropriate message is displayed.These methods provide an efficient way to compare file contents and ensure data consistency in various applications.