Comparing two strings in Python - depends on string source...?
Question: I have the following python script:

    
    
    import sys
    import io 
    
    str1 = 'asd'
    str2 - 'asd'
    if (str2.find(str1)==-1):
        print('FALSE')
    else:
        print('TRUE')
    
    #Prints "TRUE"
    

It works fine. No problem. The problem starts if I take the string, put it in
a file (save it) and then read the content of the file to str1 and str2, like
so:

    
    
    import sys
    import io 
    
    fHandler = open(r'C:\dev\1.pub','r')
    str1 = fHandler.read()
    str2 = fHandler.read()
    
    if (str2.find(str1)==-1):
        print('FALSE')
    else:
        print('TRUE')
    
    #Prints "FALSE"
    

Why is the behaviour different?

Thanks!


Answer: If you try to call `fHandler.read()` twice, then the second one will return n
empty string (or empty bytes if you read binary). `fHandler.read()` reads
everything until the end and the second call will start reading at the end
until the end, so it returns an empty string. Try adding `fHandler.seek(0)`
beetween your function calls and it should work.

