如何将变量x存储在文本文件中的特定行(第一行)上,并在程序打开时将其恢复。
另外,我如何将数据存储在特定行的文本文件中
答案 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 python,Writing a Top Score to a data file。