Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a single integer from a text file and assigning it to a variable

I have written an integer to a text file like this:

dolyen = int(input("Enter exchange rate: "))
filedolyen = open("dolyen.txt","w")
filedolyen.write("%s" % dolyen)
filedolyen.close()

Then I have tried to copy the number from the file in another section of code:

yendol1 = open("yendol.txt","r")
print(value * yendol1,"Dollars")
like image 768
Adam K Avatar asked Feb 18 '26 10:02

Adam K


1 Answers

filedolyen.read()

reads the whole file as a string

int(filedolyen.read())

to get back an integer

like image 56
Jal Avatar answered Feb 20 '26 00:02

Jal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!