Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing string to a file on a new line every time

Tags:

python

newline

I want to append a newline to my string every time I call file.write(). What's the easiest way to do this in Python?

like image 941
kaushik Avatar asked May 27 '10 03:05

kaushik


People also ask

Does Python write add a newline?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do you write a string to a text file in Python?

Python – Write String to Text FileOpen the text file in write mode using open() function. The function returns a file object. Call write() function on the file object, and pass the string to write() function as argument. Once all the writing is done, close the file using close() function.


1 Answers

Use "\n":

file.write("My String\n") 

See the Python manual for reference.

like image 151
halfdan Avatar answered Sep 18 '22 11:09

halfdan