-1

I want to compare a text file content to a string in the code

test.txt
a
b
c
d

I used print(r[0]) and print(st[0]) to virify the values however the ourput is always "No" code:

st=["a","b"]
with open("test.txt", "r+") as input:
    r =input.readlines()
    input.close()
    print(r[0])
    print(st[0])
    
    if r[0] == st[0]:
        print("yes")
    else:
        print("No")
  • Try to avoid using built-in `input` as file object. – Daniel Hao May 31 '22 at 13:23
  • 1
    Don't use `input` as a name, it is already a built-in function. And no need to close a file inside a `with` statement – Tomerikoo May 31 '22 at 13:23
  • 1
    It would be much more obvious why your two values don't match if you used `print(repr(X))` to examine them, rather than just `print(X)`. – jasonharper May 31 '22 at 13:26
  • `input.close()`: you'll want to read up what `with open ...` does, because the close line is redundant. – 9769953 May 31 '22 at 13:34
  • 1
    What is the output of your code? There are two more print functions, so it's unlikely it's only "No". – 9769953 May 31 '22 at 13:35
  • Does your input file start with `test.txt`, or with `a`? Because currently, `test.txt` is included in the same block, which makes it seem as if it's part of the contents of the input file. – 9769953 May 31 '22 at 13:36
  • Hi @Ali Herz its not same because one value having extra line. try this >>if r[0].strip() ==st[0]: print("yes") else: print("No") – Rajan May 31 '22 at 13:51

0 Answers0