python : Compare two large files

python : Compare two large files … here is a solution to the problem.

python : Compare two large files

This is Follow-up questions for Compare two large files are answered by phihag

I want to show different number of lines after comparing two files. You want to display a different number of message lines after the program completes.

My try:

with open(file2) as b:
  blines = set(b)
with open(file1) as a:
  with open(file3, 'w') as result:
    for line in a:
      if line not in blines:
        result.write(line)

lines_to_write = []
with open(file2) as b:
  blines = set(b)
with open(file1) as a:
  lines_to_write = [l for l in a if l not in blines]

print('count of lines are in difference:', len(lines_to_write))

Solution

If you can load everything into memory, you can do the following on the collection:

union = set(alines).union(blines)
intersection = set(alines).intersection(blines)
unique = union - intersection

EDIT: It’s simpler (and faster) to:

set(alines).symmetric_difference(blines)

Related Problems and Solutions