Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Compare two Files line by line in Python

  • Last Updated : 17 Feb, 2021

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:

compare fil python

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.  


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


  • 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 automcally uniformly newline free


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



# 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=''):


— file1.txt

+++ file2.txt

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








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.



line unique to sequence 1


line unique to sequence 2

‘ ‘

line common to both sequences


line not present in either input sequence


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



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





– too

– simple.

+ so

+ easy.

Method 3: Using while loop and Intersection Method


  • 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



# 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
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("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)
            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)
            print("#+", "Line-%d" % line_no, file_2_line)
        # Print a empty line
    # Read the next line from the file
    file_1_line = file_1.readline()
    file_2_line = file_2.readline()
    line_no += 1


Comparing files 

 @ file1.txt

 # file2.txt

Common Lines in Both Files




Difference Lines in Both Files

@- Line-4 too

#+ Line-4 so

@- Line-5 simple.

#+ Line-5 easy.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!