Skip to content Skip to sidebar Skip to footer

Python: Write To New File Line

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

Solution 1:

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:


Solution 2:

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')

Post a Comment for "Python: Write To New File Line"