print or write on a text file ?

Terry Reedy tjreedy at udel.edu
Fri Sep 28 20:41:38 EDT 2012


On 9/28/2012 2:42 PM, Franck Ditter wrote:
> Hi !
> Here is Python 3.3
> Is it better in any way to use print(x,x,x,file='out')
> or out.write(x) ? Any reason to prefer any of them ?

print converts objects to strings and adds separators and terminators. 
If you have a string s and want to output it as is, out.write(s) is 
perhaps faster. It is 6 chars shorted than print(s, file=out).

> There should be a printlines, like readlines ?

No, now that files are iterators, I believe readlines is somewhat obsolete.

file.readlines() == list(file)

The only reason not to deprecate it is for the hint parameter to limit 
the bytes read. That is  little harder to do with the iterator.


If you have any iterator of lines,
for line in lines: line.print()
is quite sufficient. There is little or no need for output limitation.

-- 
Terry Jan Reedy




More information about the Python-list mailing list