Compare two Files line by line in Python - GeeksforGeeks

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:
file.txt
file1.txt
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 automcally uniformly newline free
Approach
Import module
Open files
Compare using unified_diff() with appropriate attributes
Example:
Python3
# Importing difflibimport 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.
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.
‘-‘
‘+’
‘ ‘
‘?’
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.
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 Modefile_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 COunterline_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.
Picked
Python file-handling-programs
python-file-handling
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python