A question of the same kind has been answered Here
You just have to modify it a little to output the differences instead of the similarities
with open('a.txt', 'r') as fa:
with open('b.txt', 'r') as fb:
diff = set(fa).difference(fb)
diff.discard('\n')
with open('d.txt', 'w') as fc:
fc.write(''.join(diff))
Tell me if it works on your side. Took me 15.16 seconds to compare two text files of 276 824 064 bytes (285,2 MB) each, comparing a total of 16,777,216 lines of strings made up of 32 characters on an average laptop, so probably feasible for files up to Gigabytes in minutes
Here is my random text files generator program
import string
import random
def genWord(length: int = 32) -> str:
return ''.join([random.choice(string.ascii_letters) for _ in range(length)])
def main() -> None:
for letter in ['a', 'b']:
filename = letter + '.txt'
with open(filename, 'w') as file: # Opening in write mode serves as emptying the file
pass
with open(filename, 'a') as file: # 'a' Mode is for Append
for _ in range(1024 * 1024 * 8): # Number of lines
file.write(genWord(32) + '\n') # Number of letters for each pseudo-word
if __name__ == "__main__":
main()