Python Program to Compare Two Text Files

Let us look at how to compare two text files in Python in this tutorial. We will use a variety of approaches to try to solve this problem.

Let us consider the below two text files as an example:

demotextfile_1.txt:

hello this is Python-programs 
good morning all
Then give a start to it 
welcome to the greatest python coding platform

demotextfile_2.txt:

hello this is Python-programs 
Are you good at coding? 
Then give a start to it 
good morning all

Program to Compare Two Text Files in Python

Below are the ways to compare two text files in Python:

Method #1: Using filecmp.cmp() Method

Python has a filecmp module that allows you to compare files. The filecmp.cmp() function returns a three-list of matched, errors, and mismatched files. filecmp.cmp() has two modes of operation.

shallow mode: In this mode, only file metadata such as size, modified date, and so on are compared.
deep mode: The content of the files is compared in this mode.

Approach:

  • Import filecmp module using the import keyword
  • Import os module using the import keyword
  • Give the first textfile as static input and store it in a variable.
  • Give the second textfile as static input and store it in another variable.
  • Pass the above given first and second text files as arguments to the cmp() function of the filecmp module to compare metadata in shallow mode
  • Returns True if both the textfiles are the same else False
  • Pass the above given first and second text files as arguments to the cmp() function of the filecmp module to comapre content in deep mode( shallow=False)
  • Returns True if both the textfiles are the same else False.
  • The Exit of the Program.

Below is the implementation:

# Import filecmp module using the import keyword 
import filecmp
# Import os module using the import keyword 
import os
# Give the first textfile as static input and store it in a variable.
Textfile_1 = "demotextfile_1.txt"
# Give the second textfile as static input and store it in another variable.
Textfile_2 = "demotextfile_2.txt"

# Pass the above given first and second text files as arguments to the cmp() function 
# of the filecmp module to compare metadata in shallow mode
# Returns True if both the textfiles are same else False
rslt = filecmp.cmp(Textfile_1, Textfile_2)
print(rslt)

# Pass the above given first and second text files as arguments to the cmp() function 
# of the filecmp module to comapre content in deep mode( shallow=False)
# Returns True if both the textfiles are same else False
rslt = filecmp.cmp(Textfile_1, Textfile_2, shallow=False)
print(rslt)

Output:

False
False

Method #2: Using line by line Comparision

Approach:

  • Open the first text file in read-only mode using the open() function by passing filename/path, mode as arguments to it and store it in a variable.
  • Open the second text file in read-only mode using the open() function by passing filename/path, mode as arguments to it and store it in another variable.
  • Take a variable and initialize its value with 0 which represents the line number.
  • Loop in each line of the given first text file using the for loop.
  • Increment the above initialized variable value by 1 which holds the line number.
  • Loop in each line of the given second text file using the other nested for loop.
  • Check if each line of the first text file is equal/similar to the corresponding line of the second text file using the if conditional statement.
  • If it is true, then print the respective line number.
  • Print Both the lines of given two text file are the same.
  • Else print the respective line number and corresponding lines of both the text files.
  • Close the given first text file using the close() function.
  • Close the given second text file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Open the first text file in read-only mode using the open() function
# by passing filename/path, mode as arguments to it and store it in a variable.
Textfile_1 = open("demotextfile_1.txt", "r")
# Open the second text file in read-only mode using the open() function
# by passing filename/path, mode as arguments to it and store it in another variable.
Textfile_2 = open("demotextfile_2.txt", "r")
# Take a variable and initialize its value with 0 which represents the line number
i = 0
# Loop in each line of the given first text file using the for loop
for f1_line in Textfile_1:
    # Increment the above initialized variable value by 1 which holds the line number
    i += 1
    # Loop in each line of the given second text file using the other nested for loop
    for f2_line in Textfile_2:
      # Check if each line of the first text file is equal/similar to the corresponding line 
      # of the second text file using the if conditional statement
      if f1_line == f2_line:
          # If it is true, then print the respective line number 
          print("line_",i,": ")
          # Print Both the lines of given two text file are the same
          print("Both the lines of given two text file are the same")
          print()
      else:
          # Else print the respective line number and corresponding lines of 
          # both the text files
          print("line_",i,": ")
          print("Textfile_1: ", f1_line)
          print("Textfile_2: ", f2_line)
      break

# Close the given first text file using the close() function
Textfile_1.close()
# Close the given second text file using the close() function
Textfile_2.close()

Output:

line_ 1 : 
Both the lines of given two text file are the same

line_ 2 : 
Textfile_1: good morning all

Textfile_2: Are you good at coding?

line_ 3 : 
Both the lines of given two text file are the same

line_ 4 : 
Textfile_1: welcome to the greatest python coding platform

Textfile_2: good morning all