关于python:Trivia Game – 我正在尝试写入高分.txt文件

Trivia Game - I am attempting to Write to high scores .txt file

本问题已经有最佳答案,请猛点这里访问。
1
2
3
4
5
6
text_file = open("high_score.txt","w")
text_file.writelines([player_name,"'s score is:", scores])
text_file.close()
text_file = open("high_score.txt","r")
print(text_file.read())
text_file.close()

我正在尝试将高分写入.txt文件。但每次重新写入文件时。

我该如何防止这种情况发生?


我认为第一行中的第二个参数"a"应该可以完成以下工作:

1
text_file = open("high_score.txt","a")

资料来源:http://www.tutorialspoint.com/python/python_files_io.htm


text_file = open("high_score.txt","w")改为text_file = open("high_score.txt","a")

您以写入模式("w")打开了一个文件,这会导致删除以前的数据并写入新数据。如果要在不删除以前数据的情况下写入新数据,则应以追加模式("A")打开该文件。