Open In App

Compare two Files line by line in Python

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, there are many methods available to this comparison. In this Article, We’ll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules.

This article uses two sample files for implementation.

Files in use:

  • file.txt

compare fil python

  • file1.txt

compare fil python

Method 1: Using unified_diff()

Python has a Module which is specially used for comparing the differences between the files. To get differences using the difflib library, we have to call the unified_diff() function to this comparison.  

Syntax:

unified_diff(file1, file2, fromfile, tofile, lineterm)

Parameter:

  • file1: List of String such as file_1_text
  • file2: List of String such as file_2_text
  • fromfile: first file name with extension
  • tofile: second file name with extension
  • lineterm: argument to “” so that the output will be automatically uniformly newline free

Approach

  • Import module
  • Open files
  • Compare using unified_diff() with appropriate attributes

Example:

Python3




# Importing difflib
import difflib
  
with open('file1.txt') as file_1:
    file_1_text = file_1.readlines()
  
with open('file2.txt') as file_2:
    file_2_text = file_2.readlines()
  
# Find and print the diff:
for line in difflib.unified_diff(
        file_1_text, file_2_text, fromfile='file1.txt'
        tofile='file2.txt', lineterm=''):
    print(line)


Output:

— file1.txt

+++ file2.txt

@@ -1,5 +1,5 @@

 Learning

 Python

 is

-too

-simple.

+so

+easy.

Method 2: Using differ

There is one Class available for comparing the differences between the files which named as Differ inside the difflib library. This class is used for comparing sequences of lines of text, and producing human-readable differences or deltas.

Code Meaning

‘-‘

line unique to sequence 1

‘+’

line unique to sequence 2

‘ ‘

line common to both sequences

‘?’

line not present in either input sequence

Approach

  • Import module
  • Open files
  • Read contents line bt line
  • Call compare function with the use of differ class object

Example:

Python3




from difflib import Differ
  
with open('file1.txt') as file_1, open('file2.txt') as file_2:
    differ = Differ()
  
    for line in differ.compare(file_1.readlines(), file_2.readlines()):
        print(line)


Output:

Learning

Python

is

– too

– simple.

+ so

+ easy.

Method 3: Using while loop and Intersection Method

Approach

  • Open both files in read mode
  • Store list of strings
  • Start comparing both files with the help of intersection() method for common strings
  • Compare both files for differences using while loop
  • Close both files

Example:

Python3




# Open File in Read Mode
file_1 = open('file1.txt', 'r')
file_2 = open('file2.txt', 'r')
  
print("Comparing files ", " @ " + 'file1.txt', " # " + 'file2.txt', sep='\n')
  
file_1_line = file_1.readline()
file_2_line = file_2.readline()
  
# Use as a COunter
line_no = 1
  
print()
  
with open('file1.txt') as file1:
    with open('file2.txt') as file2:
        same = set(file1).intersection(file2)
  
print("Common Lines in Both Files")
  
for line in same:
    print(line, end='')
  
print('\n')
print("Difference Lines in Both Files")
while file_1_line != '' or file_2_line != '':
  
    # Removing whitespaces
    file_1_line = file_1_line.rstrip()
    file_2_line = file_2_line.rstrip()
  
    # Compare the lines from both file
    if file_1_line != file_2_line:
        
        # otherwise output the line on file1 and use @ sign
        if file_1_line == '':
            print("@", "Line-%d" % line_no, file_1_line)
        else:
            print("@-", "Line-%d" % line_no, file_1_line)
              
        # otherwise output the line on file2 and use # sign
        if file_2_line == '':
            print("#", "Line-%d" % line_no, file_2_line)
        else:
            print("#+", "Line-%d" % line_no, file_2_line)
  
        # Print a empty line
        print()
  
    # Read the next line from the file
    file_1_line = file_1.readline()
    file_2_line = file_2.readline()
  
    line_no += 1
  
file_1.close()
file_2.close()


Output:

Comparing files 

 @ file1.txt

 # file2.txt

Common Lines in Both Files

Learning

Python

is

Difference Lines in Both Files

@- Line-4 too

#+ Line-4 so

@- Line-5 simple.

#+ Line-5 easy.



Previous Article
Next Article

Similar Reads

How To Compare Two Dataframes with Pandas compare?
A DataFrame is a 2D structure composed of rows and columns, and where data is stored into a tubular form. It is mutable in terms of size, and heterogeneous tabular data. Arithmetic operations can also be performed on both row and column labels. To know more about the creation of Pandas DataFrame. Here, we will see how to compare two DataFrames with
5 min read
Compare two files using Hashing in Python
In this article, we would be creating a program that would determine, whether the two files provided to it are the same or not. By the same means that their contents are the same or not (excluding any metadata). We would be using Cryptographic Hashes for this purpose. A cryptographic hash function is a function that takes in input data and produces
3 min read
How to compare two text files in python?
Python has provided the methods to manipulate files that too in a very concise manner. In this article we are going to discuss one of the applications of the Python's file handling features i.e. the comparison of files. Files in use: Text File 1Text File 2Method 1: Comparing complete file at once Python supports a module called filecmp with a metho
3 min read
Compare Two Xml Files in Python
We are given two XML files and our task is to compare these two XML files and find out if both files are some or not by using different approaches in Python. In this article, we will see how we can compare two XML files in Python. Compare Two XML Files in PythonBelow are the possible approaches to compare two XML files in Python: Using lxml moduleU
3 min read
Compare Two Csv Files Using Python
We are given two files and our tasks is to compare two CSV files based on their differences in Python. In this article, we will see some generally used methods for comparing two CSV files and print differences. Compare Two CSV Files for Differences in PythonBelow are some of the ways by which we can compare two CSV files for differences in Python:
2 min read
Compare Keys In Two Yaml Files And Print Differences?
YAML (YAML Ain't Markup Language) files are widely used for configuring applications and storing data in a human-readable format. When dealing with multiple YAML files, it's common to compare them to identify differences, especially when managing configurations across different environments or versions. In this article, we'll explore generally see
2 min read
How to merge multiple excel files into a single files with Python ?
Normally, we're working with Excel files, and we surely have come across a scenario where we need to merge multiple Excel files into one. The traditional method has always been using a VBA code inside excel which does the job but is a multi-step process and is not so easy to understand. Another method is manually copying long Excel files into one w
4 min read
How to Compare Two Dictionaries in Python?
In this article, we will discuss how to compare two dictionaries in Python. As we all know what is a dictionary, but sometimes we may need to compare two dictionaries. Let's see different methods to do the same. Using == operator to Compare Two Dictionaries Here we are using the equality comparison operator in Python to compare two dictionaries whe
2 min read
How to Compare Two Iterators in Python
Python iterators are powerful tools for traversing through sequences of elements efficiently. Sometimes, you may need to compare two iterators to determine their equality or to find their differences. In this article, we will explore different approaches to compare two iterators in Python. Compare Two Iterators In PythonBelow are the ways to compar
3 min read
How to compare values in two Pandas Dataframes?
Let's discuss how to compare values in the Pandas dataframe. Here are the steps for comparing values in two pandas Dataframes: Step 1 Dataframe Creation: The dataframes for the two datasets can be created using the following code: C/C++ Code import pandas as pd # elements of first dataset first_Set = {'Prod_1': ['Laptop', 'Mobile Phone', 'Desktop',
2 min read
three90RightbarBannerImg