[英]Store and recall a variable in a text file Python
How would i store the variable x on a specific line (the first)in a text file and recover it as the program opens. 我如何将变量x存储在文本文件中的特定行(第一行)上,并在程序打开时恢复它。
Also how would i store data in a text file on a specific line 另外我如何将数据存储在特定行的文本文件中
You should take a look at the open()
built-in. 您应该看看内置的
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())
Next time, you should check for duplicates: 下次,您应该检查重复项:
easy save/load of data in python , Writing a Top Score to a data file . 在python中轻松保存/加载数据 , 将最高分数写入数据文件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.