Open In App

Compare two Files line by line in Python

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.



Last Updated : 21 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads