Correct way to write line to file?
I'm used to doing
print >>f, "hi there"
However, it seems that
print >> is getting deprecated. What is the recommended way to do the line above?
Regarding all those answers with
"\n"...is this universal or Unix-specific? IE, should I be doing
"\r\n" on Windows?
This should be as simple as:
with open('somefile.txt', 'a') as the_file: the_file.write('Hello\n')
From The Documentation:
Do not use
os.linesepas a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
Some useful reading:
You should use the
print() function which is available since Python 2.6+
from __future__ import print_function # Only needed for Python 2 print("hi there", file=f)
For Python 3 you don't need the
import, since the
print() function is the default.
The alternative would be to use:
f = open('myfile', 'w') f.write('hi there\n') # python will convert \n to os.linesep f.close() # you can omit in most cases as the destructor will call it
Quoting from Python documentation regarding newlines:
On output, if newline is None, any
'\n'characters written are translated to the system default line separator,
os.linesep. If newline is
'', no translation takes place. If newline is any of the other legal values, any
'\n'characters written are translated to the given string.
Read more... Read less...
The python docs recommend this way:
with open('file_to_write', 'w') as f: f.write('file contents\n')
So this is the way I usually do it :)
Statement from docs.python.org:
It is good practice to use the 'with' keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks.
Here is an exact unedited Python 2.7.1 interpreter session on Windows:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.linesep '\r\n' >>> f = open('myfile','w') >>> f.write('hi there\n') >>> f.write('hi there' + os.linesep) # same result as previous line ????????? >>> f.close() >>> open('myfile', 'rb').read() 'hi there\r\nhi there\r\r\n' >>>
As expected, os.linesep does NOT produce the same outcome as
'\n'. There is no way that it could produce the same outcome.
'hi there' + os.linesep is equivalent to
'hi there\r\n', which is NOT equivalent to
It's this simple: use
\n which will be translated automatically to os.linesep. And it's been that simple ever since the first port of Python to Windows.
There is no point in using os.linesep on non-Windows systems, and it produces wrong results on Windows.
DO NOT USE os.linesep!
In Python 3 it is a function, but in Python 2 you can add this to the top of the source file:
from __future__ import print_function
Then you do
print("hi there", file=f)
If you are writing a lot of data and speed is a concern you should probably go with
f.write(...). I did a quick speed comparison and it was considerably faster than
print(..., file=f) when performing a large number of writes.
import time start = start = time.time() with open("test.txt", 'w') as f: for i in range(10000000): # print('This is a speed test', file=f) # f.write('This is a speed test\n') end = time.time() print(end - start)
write finished in 2.45s on my machine, whereas
If you choose to go with
print(..., file=f) you will probably find that you'll want to suppress the newline from time to time, or replace it with something else. This can be done by setting the optional
end parameter, e.g.;
with open("test", 'w') as f: print('Foo1,', file=f, end='') print('Foo2,', file=f, end='') print('Foo3', file=f)
Whichever way you choose I'd suggest using
with since it makes the code much easier to read.
Update: This difference in performance is explained by the fact that
write is highly buffered and returns before any writes to disk actually take place (see this answer), whereas
start = start = time.time() long_line = 'This is a speed test' * 100 with open("test.txt", 'w') as f: for i in range(1000000): # print(long_line, file=f) # f.write(long_line + '\n') end = time.time() print(end - start, "s")
The performance difference now becomes much less pronounced, with an average time of 2.20s for
write and 3.10s for