CSV files, lists

I am trying to run a highscores system that adds the score for each round of a game to a list, sorts it into highest to lowest and then writes it to a csv file. The aim is to keep an all time record of the best score from the game. The code that i have got adds the highscores to a list, which is sorted and added into a csv file. The issue that im having is that rather than adding each new score, when the game is played again, to the end of the list, it is just replacing the score from the previous game. Anyone got any tips?

Thanks, Danny.

How are you opening the file? Are you opening it for appending or for writing?

Here’s an example:

import csv

csv_path = 'path/to/csv/file'

with open(csv_path, 'a', newline='') as file:
    csv.writer(file).writerow(['row 1'])

with open(csv_path, 'a', newline='') as file:
    csv.writer(file).writerow(['row 2'])

Not without you showing us the code, no.

Im ussing

with open(‘highscorers.csv’, ‘r+’) as file:
csv.writer(file).writerow(row[0])

Hi,

fyi …

'r+' Both read and write to an existing file. The file pointer will be at the beginning of the file.

'a+' Both read and write to an existing file or create a new file. The file pointer will be at the end of the file.

'a' Append: Write data to the end of an existing file. Else creates a new file.

'r' Read only: the default. The stream is positioned at the beginning of the file.

'w' Write: Create a new file or overwrite the contents of an existing file.