Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)

Let's say I have two CSV files and In the third CSV file, I want to print the difference between those CSV files.

First CSV has an old list of hash

Second CSV has the new list of hash which will have both old and new hash.

This is my code:

import csv

t1 = open('old.csv', 'r')

t2 = open('new.csv', 'r')

fileone = t1.readlines()

filetwo = t2.readlines()



outFile = open('update.csv', 'w')

x = 0

for i in fileone:

    if i != filetwo[x]:


    x += 1


I don't want to use the diff

1 Answer

0 votes
by (26.4k points)

Since you are comparing each line in fileone to filetwo, that will become a problem. If in case, there's one extra line in one file, here the lines won't be equal again.

Try this code:

with open('old.csv', 'r') as t1, open('new.csv', 'r') as t2:

    fileone = t1.readlines()

    filetwo = t2.readlines()

with open('update.csv', 'w') as outFile:

    for line in filetwo:

        if line not in fileone:


Interested to learn more about Python? Come & join: python course course 

Browse Categories