Skip to content Skip to sidebar Skip to footer

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.clos

Solution 1:

filedolyen.read()

reads the whole file as a string

int(filedolyen.read())

to get back an integer


Solution 2:

We can use with so the file resource is auto-closed

with open('myfile') as file:
     myint = int(file.read())

Post a Comment for "Retrieving A Single Integer From A Text File And Assigning It To A Variable"