I have this part of code that I originally couldn't get to work:
with open ("Names.txt" , "r+") as f:
f.seek(0)
if f.read() == " ":
print("No text")
else:
print("Got text")
It is supposed to check whether the text file has any, well, text.
However, when I would run this code, it would always return "Got text", even if the file was empty.
I found a way to make it work:
with open ("Names.txt" , "r+") as f:
f.seek(0)
if not f.read():
print("No text")
else:
print("Got text")
So my question is, why does the second piece of code work while the first doesn't?