Reputation: 317
I have two files, file1 contains contents as
aaa
bbb
ccc
and file 2 contains contents as
ccc
ddd
eee
aaa
rrr
bbb
nnn
I would like to do like this, if file2 contains file1's line, then that line would be removed from file2. At last, file2 will be as ddd eee rrr nnn Besides, my code is
f1 = open("test1.txt","r")
f2 = open("test2.txt","r")
fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
outFile = open("test.txt","w")
x = 0
for i in fileOne:
if i != fileTwo[x]:
outFile.writelines(fileTwo[x])
x += 1
outFile.close()
Thank you.
Upvotes: 0
Views: 4169
Reputation: 304205
with open("f1.txt") as f1:
s1 = set(f1)
with open("f2.txt") as f2, open("f3.txt","w") as f3:
f3.writelines(x for x in f2 if x not in s1)
It's good practice to use a context manager to close the file (this is what the with
does).
It's much more efficient to check for membership of a set
than a list
If there is a possibility of extra whitespace, you should strip the lines like this
with open("f1.txt") as f1:
s1 = set(x.strip() for x in f1)
with open("f2.txt") as f2, open("f3.txt","w") as f3:
f3.writelines(x for x in f2 if x.strip() not in s1)
Upvotes: 4
Reputation: 7809
Using your code...
f1 = open("test1.txt","r").read()
f2 = open("test2.txt","r").read()
fileOne = f1.splitlines()
fileTwo = f2.splitlines()
# remove the dup lines
nodup_lines = [line for line in fileTwo if line not in fileOne]
# join using newline character
newFileTwo = '\n'.join(nodup_lines)
# write file
outFile = open("test.txt","w")
outFile.write(newFileTwo)
outFile.close()
Upvotes: 0
Reputation: 2555
Use set difference to find the differences of the two files.
f1 = open("test1.txt","r").readlines()
f2 = open("test2.txt","r").readlines()
diff = set(f2) - set(f1)
outFile = open("test.txt","w")
outFile.writelines(line for line in f2 if line in diff)
Upvotes: 0