0

I'm working on a little side project and I have a small issue currently. The user enters their name through a input, and then it is saved to a text file. Everything works fine but when it comes to making a new line with \n to separate the names, it replaces the previous text in the text file. I can't seem to find the reason why online. Any help would be appreciated!

name = input("Name: "))

with open("names.txt", "w") as w:
    w.write(name + "\n") 
Slender
  • 35
  • 7

1 Answers1

4

You are using the wrong flag, w means writing over any existing content, the solution is to use a for appending new content,

with open("names.txt", "a") as w:

reference: w3school

kennysliding
  • 2,783
  • 1
  • 10
  • 31