Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30425 > unrolled thread

print or write on a text file ?

Started byFranck Ditter <franck@ditter.org>
First post2012-09-28 20:42 +0200
Last post2012-10-01 10:22 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  print or write on a text file ? Franck Ditter <franck@ditter.org> - 2012-09-28 20:42 +0200
    Re: print or write on a text file ? Wayne Werner <wayne@waynewerner.com> - 2012-09-28 16:33 -0500
    Re: print or write on a text file ? Terry Reedy <tjreedy@udel.edu> - 2012-09-28 20:41 -0400
    Re: print or write on a text file ? nn <pruebauno@latinmail.com> - 2012-10-01 10:22 -0700

#30425 — print or write on a text file ?

FromFranck Ditter <franck@ditter.org>
Date2012-09-28 20:42 +0200
Subjectprint or write on a text file ?
Message-ID<franck-20D159.20420228092012@news.free.fr>
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 ?
There should be a printlines, like readlines ?
Thanks,

    franck

[toc] | [next] | [standalone]


#30429

FromWayne Werner <wayne@waynewerner.com>
Date2012-09-28 16:33 -0500
Message-ID<mailman.1584.1348868015.27098.python-list@python.org>
In reply to#30425
On Fri, 28 Sep 2012, 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 ?
> There should be a printlines, like readlines ?
> Thanks,

The print function automatically appends newlines to the end of what it 
prints.

So if you had

text = 'Hello!'

and you did:

print(text, file=outfile)

then outfile would contain 'Hello!\n'

In contrast, outfile.write(text) would only write 'Hello!'. No newline.

There are lots of other handy things you can do with the print function:

values = [1,2,3,4]
print(*values, sep='\n', file=outfile)

I'll leave it to you to experiment.
HTH,
Wayne

[toc] | [prev] | [next] | [standalone]


#30441

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-28 20:41 -0400
Message-ID<mailman.1592.1348879313.27098.python-list@python.org>
In reply to#30425
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

[toc] | [prev] | [next] | [standalone]


#30618

Fromnn <pruebauno@latinmail.com>
Date2012-10-01 10:22 -0700
Message-ID<a47f021d-8c28-41f7-ad32-431696557780@a7g2000yqo.googlegroups.com>
In reply to#30425
On Sep 28, 2:42 pm, Franck Ditter <fra...@ditter.org> 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 ?
> There should be a printlines, like readlines ?
> Thanks,
>
>     franck

There is

out.writelines(lst)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web