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
What you are doing wrong is -
Please note
a
andb
are still file descriptors, once you doset(a)
, if you doset(a)
again, you will get an empty set, because in the first call toset(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 -Also, you should flush the file to which you write , after completing the write and at the end close all files as -