0

how do I write to a file and not removing the file/the content of the file

values = '113'
with open("file.txt", 'w') as output:
    for row in values:
        output.write(str(row) + '\n')

how do I fix it?

and I planing to use it as a score board of my snake game I made in pygame

Aven Desta
  • 2,114
  • 12
  • 27
zaze
  • 37
  • 1
  • 1
  • 6
  • basically you want to append to a file. This behaviour can be achieved by using the mode `a` insteaf of `w`. This beeing said: This question has already been answered e.g. here https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python . – newBee Feb 13 '20 at 16:31
  • Does this answer your question? [How do you append to a file in Python?](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python) – newBee Feb 13 '20 at 16:31

1 Answers1

1

Using the 'append' attribute ('a') in the open function:

values = '113'
with open("file.txt", 'a') as output:
    for row in values:
        output.write(str(row) + '\n')
Luis Munoz
  • 146
  • 4