Writing string to a file on a new line every time
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?
Solution 1:
Use "\n":
file.write("My String\n")
See the Python manual for reference.
Solution 2:
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
Solution 3:
You can use:
file.write(your_string + '\n')