Finding difference is not working

93 views Asked by At

I am trying to find difference from two files but still I get answer from two files

This is my code

#File one(This file contents should be removed after comparing file two)
a = open('diff1','r+')
#File two
#This file has 999999 records
b = open('gtin','r+')
f8 = open('missing-test-remove.txt','w')
def diff(a, b):
    c = set(a).union(set(b))
    d = set(a).intersection(set(b))
    result =  list(c - d)
    for s in result:
        print s
        f8.write(s)

diff(a,b)

But still I get same results from two files but file one contents should be removed after comparing with file two

2

There are 2 answers

0
Anand S Kumar On BEST ANSWER

What you are doing wrong is -

c = set(a).union(set(b))
d = set(a).intersection(set(b))

Please note a and b are still file descriptors, once you do set(a) , if you do set(a) again, you will get an empty set, because in the first call to set(a) , the complete file has already been read and the cursor for the file is at the end.

You need to change your code such that you only call set(a) and `set(b) once. Something along the lines of -

#File one(This file contents should be removed after comparing file two)
a = open('diff1','r+')
#File two
#This file has 999999 records
b = open('gtin','r+')
f8 = open('missing-test-remove.txt','w')
def diff(a, b):
    sa = set(a)
    sb = set(b)
    c = sa.union(sb)
    d = sa.intersection(sb)
    result =  list(c - d)
    for s in result:
        print s
        f8.write(s)

diff(a,b)

Also, you should flush the file to which you write , after completing the write and at the end close all files as -

a.close()
b.close()
f8.close()
0
yushulx On

You need to save the set value. A simple test:

    print a
    print set(a)
    print a
    print set(a) # wrong

So

    seta = set(a)
    setb = set(b)
    setc = seta.union(setb)
    setd = seta.intersection(setb)