I\'m aiming to write a script that will compare each line within a file, and based upon this comparison, create a new file containing the lines of text which aren\'t in the
with open("H:/Ast/Hpa.java", encoding="utf8") as f:
with open("G:/Soft_install/Hpa.java", encoding="utf8") as fe:
for line in f:
for linefe in fe:
if (line != linefe):
print(line)
break
else:
break
This is working for me:
def compare(File1,File2):
with open(File1,'r') as f:
d=set(f.readlines())
with open(File2,'r') as f:
e=set(f.readlines())
open('file3.txt','w').close() #Create the file
with open('file3.txt','a') as f:
for line in list(d-e):
f.write(line)
You need to compare the readlines set and find out lines that are not present in file2. You can then append these lines to the new file.