What's the difference between using File.write()
and print>>File,
?
Which is the pythonic way to write to file?
>>> with open('out.txt','w') as fout:
... fout.write('foo bar')
...
>>> with open('out.txt', 'w') as fout:
... print>>fout, 'foo bar'
...
Is there an advantage when using print>>File,
?
write()
method writes to a buffer, which (the buffer) is flushed to a file whenever overflown/file closed/gets explicit request (.flush()
).
print
will block execution till actual writing to file completes.
The first form is preferred because its execution is more efficient. Besides, the 2nd form is ugly and un-pythonic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With