0

im trying to create a leader board for a game im making. im trying to write the most recent high score to the top line of the text file above other lines, what should i do?

heres my code:

s = open("highscore.txt", 'r')
scores = s.read()
scores = int(scores)

if score > (scores):
    highscore = score
    print("NEW HIGH SCORE")

    s = open("highscore.txt", 'a')
    s.write(str(highscore))
    s.write("\n")
    s.close()
    s = open("highscore.txt", 'r')
    scores = s.read()
    scores = int(scores)
    s.close()
  • You could look [here](https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file) – Maxiboi Sep 01 '20 at 18:43
  • Does this answer your question? [Prepend line to beginning of a file](https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file) – Arafat Hasan Sep 01 '20 at 18:49

1 Answers1

0

The most straight forward way I think would be to open a second high score file in which you first write the new score, and then the contents of the old high score file.

Then remove the old file and rename the new one.

Karl
  • 331
  • 1
  • 6