Reputation: 121
I have been looking around to see if I can find a way to write to a new line in a file every time the user inputs. The basic code is this:
while True:
f = open(server,"w")
initchat = str(input("Chat: "))
chat = str((user + " - " + initchat))
f.write(chat)
f.write("\n")
f.close
Many of the answers have been to add \n to the string but that only adds a new line after and doesn't allow the new line to be written to. Do I make any sense? Any help would be much appreciated Thanks
Upvotes: 0
Views: 112
Reputation: 6579
The problem is this:
f = open(server,"w")
The above will only write 1 line:
You have to use "a"
for append.
f = open(server,"a")
BTW: you also have to indent your code after while:
Upvotes: 1
Reputation: 5972
You should do a simple job:
with open('file.txt', 'a+') as f:
initchat = str(input("Chat: "))
chat = str((user + " - " + initchat))
f.write(chat+'\n')
Upvotes: 0