Float must be a string or a number?

money is a file object, not the content of the file. To get the content, you have to read the file. If the entire file contains just that one number, then read() is all you need.

moneyx = float(money.read())

Otherwise you might want to use readline() to read a single line or even try the csv module for more complex files.

Also, don’t forget to close() the file when you are done, or use the with keyword to have it closed automatically.

with open("money.txt") as money:
    moneyx = float(money.read())
print(moneyx)

Leave a Comment

tech