Malik
Malik

Reputation: 11

How to enter text into a new line while writing to a file?

Whenever I execute this piece of code more than once, my data is stored, but not in separate lines every time the user inputs a new data.

I have already tried to use "/n", but in lieu of a new line, the command "/n" itself gets written to my file.

What can I do to enter the data in separate new lines?

file = open(f"{fileName}.txt", "a")
file.write("Text to write...")
file.close()

Upvotes: 0

Views: 53

Answers (2)

Yuval.R
Yuval.R

Reputation: 1306

You need to use \n and not /n, like so:

file = open(f"{fileName}.txt", "a")
file.write("\nText to write...")
file.close()

Upvotes: 1

Tom Lee
Tom Lee

Reputation: 366

You used the wrong slash, the correct one is
So it is \n

Upvotes: 1

Related Questions