Python3. CSV. How do I write a new row to the csv file without overwriting the current row?

I am running a word guessing game. Once the word has been guessed, I want to add the details of myName, dtFullDate, attempts & wordChosen to a .csv file. This works, however if I run the game again, the previous row in the .csv gets overwritten. I've tried if statements for when a row has content to move to the next line however i was unsuccessful.

 outputFile = open('scoreboard.csv', 'w', newline='')
 outputWriter = csv.DictWriter(outputFile, ['Name', 'Date', 'Attempts', 'Word Chosen'])
 outputWriter.writeheader()
 outputWriter.writerow({'Name' : myName, 'Date' : dtFullDate, 'Attempts' : attempts, 'Word Chosen' : wordChosen})
 #outputWriter.writerow([myName, attemts, wordChosen, timestamp])
 outputFile.close()

 # Display the scoreboard from the scoreboard.csv file
  print(("************* scoreboard *************\n".upper()))
  outputFile = open('scoreboard.csv')
  outputDictReader = csv.DictReader(outputFile, ['Name', 'Date', 'Attempts', 'Word Chosen'])
  for row in outputDictReader:
    print([myName], [dtFullDate], [attempts], [wordChosen])
  #outputWriter.writerow([myName, dtFullDate, attempts, wordChosen])
  outputFile.close()
  break

Solution 1:

Open the file with access mode 'a' instead of 'w', so that you append content to it.

Also, it's good practice to open files using the with statement, so that the file is automatically closed when the block finishes executing.

with open('scoreboard.csv', 'a', newline=' ') as output_file:
    # your code down to `output_file.close()` (without it) goes here

By the way, why do you have a break at the very end of your code?