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

Asked

Viewed 867 times

0

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

How do I convert the contents of the . txt file into integer so I can do operations with it? in case you have no way to do this, as I can record data without losing them when restarting my program?

1 answer

0


In this way:

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

When you use the open with with it will already close the file. So I used this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.