0
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()

I am attempting to write high scores to a .txt file. But each time it re-writes the file.

How do I keep this from happening?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
SleepyViking
  • 59
  • 12

2 Answers2

1

change text_file = open("high_score.txt", "w") to text_file = open("high_score.txt", "a")

You opened a file in writing mode ('w') which causes to erase previous data and write new data. If you want to write new data without erasing the previous data, you should open the file in append mode ('a')

Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
1

I think that "a" as second parameter in the first line should do the work:

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

Source: http://www.tutorialspoint.com/python/python_files_io.htm

Tehuan
  • 71
  • 1
  • 6