在文本文件Python中存储和调用变量

时间:2014-10-27 19:47:40

标签: python python-3.x python-3.4

如何将变量x存储在文本文件中的特定行(第一行)上,并在程序打开时将其恢复。

另外,我如何将数据存储在特定行的文本文件中

1 个答案:

答案 0 :(得分:1)

您应该查看内置的open()

#To write to a file:
with open("file.txt", "w") as f:
    f.write("Data!")

#To read from a file:
with open("file.txt", "r") as f:
    print(f.read())

下次,您应检查重复项:

easy save/load of data in pythonWriting a Top Score to a data file