tags:

views:

49

answers:

3

i want to write a string to a file and every time i use file.write( )it should write on a new line in my file

please tell how can i do this

+1  A: 

You can do this in two ways:

f.write("text to write\n")

or, depending on your Python version (2 or 3):

print >>f, "text to write"         # Python 2.x
print("text to write", file=f)     # Python 3.x
Greg Hewgill
i am using f.writelines(str(x))to write into a file where x is listto now tell how to write a list x into a file coping each list starting at new line
kaushik
@kaushik: f.write('\n'.join(x)) or f.writelines(i + '\n' for i in x)
Steven
+1  A: 

Use "\n":

file.write("My String\n");

See the Python manual for reference.

halfdan
A: 

Maybe can you use

file.write(your_string + '\n')
Krishna K