money = open("money.txt", "r")moneyx = float(money)print(moneyx)The message file, money.txt, includes only this:

0.00The error message I receive is:

TypeError: float() argument must it is in a cable or a numberIt is most most likely a basic mistake. Any advice? ns am using Python 3.3.3.

You are watching: Typeerror: float() argument must be a string or a number


*

*

money is a document object, not the contents of the file. To gain the content, you have to read the file. If the entire file contains just that one number, climate read() is every you need.

moneyx = float(money.read())Otherwise you could want to usage readline() to review a solitary line or even shot the csv module for more complex files.

Also, don"t forget to close() the paper when you space done, or use the through keyword to have it close up door automatically.

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

*

*

Money is a file, not a string, thus you cannot convert a whole file to a float. Rather you have the right to do something prefer this, whereby you check out the whole record into a list, wherein each heat is an object in the list. You would loop through and also convert it that way.

money = open("money.txt", "r")lines = money.readlines()for l in lines: moneyx = float(l) print(moneyx)

*

It"s better practice to use "with" when opening a file in python. This means the document is implicitly closed ~ the procedure is done

with open("money.txt", "r") together f: contents = f.readlines() for line in content: publish float(line)
thanks for contributing an answer to ridge Overflow!

Please be certain to answer the question. Provide details and also share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based upon opinion; earlier them up with references or personal experience.

See more: Shadow Of War Web Of Fate - Web Of Fate Issue: Shadowofmordor

To discover more, check out our tips on writing good answers.


write-up Your answer Discard

By clicking “Post your Answer”, you agree come our regards to service, privacy policy and also cookie policy


Not the answer you're spring for? Browse various other questions tagged python type-conversion text-files or questioning your own question.


site design / logo design © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.11.40730


your privacy

By click “Accept all cookies”, you agree ridge Exchange deserve to store cookies on your device and disclose info in accordance with our Cookie Policy.