Python, writing an integer to a '.txt' file

前端 未结 5 1224
礼貌的吻别
礼貌的吻别 2021-02-20 03:20

Would using the pickle function be the fastest and most robust way to write an integer to a text file?

Here is the syntax I have so far:

import pickle

p         


        
5条回答
  •  太阳男子
    2021-02-20 04:07

    I just encountered a similar problem. I used a simple approach, saving the integer in a variable, and writing the variable to the file as a string. If you need to add more variables you can always use "a+" instead of "w" to append instead of write.

    f = open("sample.txt", "w")
    integer = 10
    f.write(str(integer))
    f.close()
    
    

    Later you can use float to read the file and you wont throw and error.

提交回复
热议问题