Reputation: 640
I have a text file (buy_sell.txt) containing the word "BOUGHT
". No new lines or spaces.
When I try to check if the contents of the file ("BOUGHT
") are equal to "BOUGHT" it evaluates to false!
f = open("buy_sell.txt", "r")
print(f.read())
if(f.read() == "BOUGHT"):
print('works')
How do I get the code to evaluate to true?
Upvotes: 0
Views: 50
Reputation: 995
Since your file is a single line, you just need to read
it once:
f = open("buy_sell.txt", "r")
if f.read() == "BOUGHT":
print("works")
If you would like to reuse this value later on, just assign it to a variable:
f = open("buy_sell.txt", "r")
my_value = f.read()
if my_value == "BOUGHT":
print("works")
if my_value != "BOUGHT":
print("Must be SOLD!")
Upvotes: 4