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.

0

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.