0

I am trying to compare a string with the text from a text file. For some reason though, it's coming back as not the same even though I've literally copied and pasted the text from the text file to my string. I also checkhow to compare a string with a text file to make sure I was doing it right and I am so I am super confused on why this isn't working.

string = "This is working"
x = open('work.txt').read()
print(string)
print(x)
print(x is string)

The content of the text file is This is working and when I run the code I get the output below

This is working
This is working

False

Edit: I also already tried:

if string == open('work.txt').read():
    print("Working")
else:
    print("Not working")

and that also gives out Not working

11
  • note that is and == are not the same thing. you can read more about the difference in the docs Commented Feb 19, 2021 at 23:10
  • @SuperStew I tried both already and it shows up false for both of them. Commented Feb 19, 2021 at 23:14
  • Please supply the expected minimal, reproducible example (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Your posted code fails to run because you didn't include your input data. Commented Feb 19, 2021 at 23:14
  • We expect you to perform basic diagnosis to include with your post. At the very least, print the suspected values at the point of error and trace them back to their sources. In many cases, doing this basic diagnosis will show you where the problem lies, and you won't need Stack Overflow at all. Since you failed to print and check types on the data values in question, we really have no way to work with your claim that == also fails. Did you perhaps include a line feed in your read? Commented Feb 19, 2021 at 23:16
  • Replacing is for == works for me also, pretty sure that is the only problem Commented Feb 19, 2021 at 23:17

2 Answers 2

2

Two issues, as mentioned you should be using == instead of is to check for equality. Also, note the extra line between the second print statements output and your result output. That's because the string you read from the file has a newline in it, so they're not the same string.

If you strip off the newline, they should compare:

string = "This is working"
x = open('work.txt').read().strip()
print(string)
print(x)
print(x == string)
Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct answer. Thank you. I edited my question to show that I was using both == and is and had originally started with == but it wasn't working so I added is as well. How does the newline get added? That text file is literally just a copy and paste.
I can only guess. It's possible whatever editor you used adds a newline to the final line of the file. It's also possible you hit Enter after Ctrl+V without thinking and saved it. Whatever the case, clearly there's a newline in the file.
I think Emacs must have just added it on its own because I actively tried deleting it just now and it kept coming back. Thank you for this.
-1

try this instead

print(x == string)

2 Comments

Also shows up false. I tried that as well already.
x has a linefeed character at the end, hence the blank line before False.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.