I am new to Python. I want to compare two files (1.txt and 2.txt).
Content of 1.txt:
a
b
c
Content of 2.txt:
a
b
c
d
the program code:
with open("1.txt") as f1:
with open("2.txt") as f2:
for line in f2.readlines():
if line not in f1.readlines():
print(line)
when I run the code, the output is:
b
c
d
In my opinion, it should only output the "d", which the letter exits in 2.txt, not in the 1.txt. So, anyone can tell why the output like in the picture?
Then I debug the program, and watches the two variables: "f1.readlines()" and "f2.readlines()" in the right corner.
I use the "Step Over" to the line 3, in the watches window, "f1.readlines()" and f2.readlines()" is still null, I can't figure it.
When i want to use "Step Over" to the line 4,the window becomes this:
all the avriables are not available
So,my question is :
1 why my code can't work?
2 what is the right code to compare the "1.txt" and "2.txt"?
Thanks!