How to compare 2 txt files in Python -


i have written program compare file new1.txt new2.txt , lines there in new1.txt , not in new2.txt has written difference.txt file.

can please have , let me know changes required in below given code. code prints same value multiple times.

file1 = open("new1.txt",'r')         file2 = open("new2.txt",'r')     newfile = open("difference.txt",'w')    line1 in file1:         line2 in file2:             if line2 != line1:                 newfile.write(line1)     file1.close()     file2.close() newfile.close() 

here's example using with statement, supposing files not big fit in memory

# open 'new1.txt' f1, 'new2.txt' f2 , 'diff.txt' outf open('new1.txt') f1, open('new2.txt') f2, open('diff.txt', 'w') outf:      # read lines 'new2.txt' , store them python set     lines = set(f2.readlines())      # loop through each line in 'new1.txt'     line in f1:          # if line not in 'new2.txt'         if line not in lines:              # write line output file             outf.write(line) 

the with statement closes opened file(s) automatically. these 2 pieces of code equal:

with open('temp.log') temp:     temp.write('temporary logging.')  # equal to:  temp = open('temp.log') temp.write('temporary logging.') temp.close() 

yet other way using 2 sets, again isn't memory effecient. if files big, wont work:

# again, open 3 files f1, f2 , outf open('new1.txt') f1, open('new2.txt') f2, open('diff.txt', 'w') outf:      # read lines in 'new1.txt' , 'new2.txt'     s1, s2 = set(f1.readlines()), set(f2.readlines())      # `s1 - s2 | s2 - s2` returns differences between 2 sets     # loop through different lines     line in s1 - s2 | s2 - s1:          # , output different lines         outf.write(line) 

keep in mind, last code might not keep order of lines


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -