asked 139k views
3 votes
Design the program that reads two files and compares their contents. The program calls a method that reads the file one line at a time. If the lines are the same the program skips the line. If the lines are different then the program prints the line number followed by the different lines from each file.

asked
User Shivanka
by
8.3k points

1 Answer

5 votes

Answer:

file1 = []

file2 = []

fileone = "lyric1.txt"

filetwo = "lyric2.txt"

def linereader( myfile, mylist):

for line in open(myfile, "r") as file:

line_read = readlines( line)

mylist.append( line_read)

file.close()

linereader( fileone, file1)

linereader( filetwo, file2)

# Assuming both files have same number of lines.

for index, item in enumerate(zip(file1, file2)):

if item[0] != item[1]:

print( index + 1, item)

Step-by-step explanation:

The python code compares two files and returns the number and the lines from both files that do not match. The source code opens the files lyric1.txt and lyric2.txt reading each lines to different lists which are iterated to compare its items returning the number of the line and the unequal lines.

answered
User MYE
by
8.5k points
Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.