How to convert a saved string to a .txt file, to an integer?

0

Example:
test = open ('test.txt', 'w')
test.write ('6000')
test.close ()

How do I convert the contents of the .txt file to integer in order to be able to do operations with it? if you can not do this, how can I save data without losing it when restarting my program?

    
asked by anonymous 28.02.2018 / 18:02

1 answer

0

In this way:

with open('data.txt', 'r') as file:
    data=int(float( file.read().replace('\n', '') ))

When you use open with with it will already close the file. That's why I used it this way.

    
28.02.2018 / 19:58