0

I'm practicing with files in python, but all of my text comes out on the same line, even if I use '\n' (in which case, only the first string appears. So:

with open('test2.txt', 'w') as f:
    f.write("I'm trying to write ...")
    f.write('new content on each line ...')
    f.write('but it all stays on the first line ...')
    f.write('Any insight would be appreciated.')

... produces:

I'm trying to write ...new content on each line ...but it all stays on the first line ...Any insight would be appreciated.

2 Answers2

1

You have to use \n character to create a new line. Example:

f.write("Line one...\n")
f.write("Line two...\n")

Good luck!

Liam Lutton
  • 204
  • 2
  • 8
0

Iy you want to append the newline to the existing lines in the file then you need to open the file by a accessing mode. This helps you to append the line.

**with open('test2.txt', 'a') as f**

**a+** - append and read
**a** - just append.