print or write on a text file ?

Discussion in 'Python' started by Franck Ditter, Sep 28, 2012.

  1. 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
     
    Franck Ditter, Sep 28, 2012
    #1
    1. Advertising

  2. Franck Ditter

    Wayne Werner Guest

    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
     
    Wayne Werner, Sep 28, 2012
    #2
    1. Advertising

  3. Franck Ditter

    Terry Reedy Guest

    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
     
    Terry Reedy, Sep 29, 2012
    #3
  4. Franck Ditter

    nn Guest

    On Sep 28, 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 ?
    > There should be a printlines, like readlines ?
    > Thanks,
    >
    >     franck


    There is

    out.writelines(lst)
     
    nn, Oct 1, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,992
    Kevin Spencer
    Apr 20, 2005
  2. Arun Kumar Srinivasan

    Using javax.print classes to print text/file

    Arun Kumar Srinivasan, Jan 13, 2004, in forum: Java
    Replies:
    5
    Views:
    6,255
    Gregory A. Swarthout
    Jan 14, 2004
  3. keto
    Replies:
    0
    Views:
    1,042
  4. Lew
    Replies:
    0
    Views:
    649
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    398
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page