Compare two Files line by line in Python - GeeksforGeeks (2024)

# 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()

Compare two Files line by line in Python - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6526

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.