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


Groups > comp.lang.python > #30429

Re: print or write on a text file ?

Date 2012-09-28 16:33 -0500
From Wayne Werner <wayne@waynewerner.com>
Subject Re: print or write on a text file ?
References <franck-20D159.20420228092012@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.1584.1348868015.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web