Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python '==' incorrectly returning false

I'm trying to get a difference of two files line by line, and Python is always returning false; even when I do a diff of the same files, Python (almost) always returns false. Goofy example, but it replicates my problem on Python 3.4.3.

file1.txt (example)
1
2
3

file1 = r"pathtofile\file1.txt"
file2 = r"pathtofile\file1.txt"
f1 = open(file1, "r")
f2 = open(file2, "r")

for line1 in f1:
    found = False
    for line2 in f2:
        if repr(line1) == repr(line2):
            found = True
            print("true")
    if found == False:
        print("false")

Python correctly identifies that the first line is the same, but everything after that is false. Can anybody else replicate this? Any ideas?

like image 405
404usernotfound Avatar asked Dec 15 '22 13:12

404usernotfound


1 Answers

You have exhausted the iterator after the first iteration over f2, you need to file.seek(0) to go back to the start of the file.

for line1 in f1:
    found = False
    for line2 in f2:
        if repr(line1) == repr(line2):
            print("true")
    f2.seek(0) # reset pointer to start of file

You only check the first line of f1 against the lines of f2, after the first loop there is nothing to iterate over.

Depending on what you want to happen, you either need to break when you find the line that matches or else reset found = False in the inner loop.

If you want all the matching lines then just store the output in a list or if the files are not very big you can use sets to find to find common lines.

with open("f1") as f1, open("f2") as f2:   
    st = set(f1)
    common = st.intersection(f2)

If you want the difference use st.difference(f2), for lines in either both not in both use st.symmetric_difference(f2). It all depends on what you actually want to do.

You might also want to check out filecmp and difflib

like image 112
Padraic Cunningham Avatar answered Jan 09 '23 00:01

Padraic Cunningham