繁体   English   中英

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

[英]Store and recall a variable in a text file Python

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

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

您应该看看内置的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())

下次,您应该检查重复项:

在python中轻松保存/加载数据将最高分数写入数据文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM