print shell output in a file

Discussion in 'Python' started by Juergen Huber, Jun 30, 2006.

  1. hello,

    one more question i will have!

    now i have written a little programm, which delivers me an output on the
    shell!


    here is the print command, which delivers me the following output (see
    below) on the shell:


    print '%-30s | %-12d | %-12d |%-12d ' % (typename,
    size / count,
    count,
    size)



    --------------NAME-------------|Groesse(Byte)-|----Anzahl----|-Gesamtgroesse
    (Byte)-|
    ----------------------------------------------------------------------------
    ----------
    ADD_REAL_N6 | 4 | 1 |4
    AND_BOOL_N10 | 4 | 1 |4
    AND_BOOL_N12 | 4 | 1 |4



    Is there a way to put this output in an file?!?! i searched about 2h for
    this, but i couldn`t find an answer!

    thnks, juergen
    Juergen Huber, Jun 30, 2006
    #1
    1. Advertising

  2. Juergen Huber

    tac-tics Guest

    It sounds like you want to use print >>

    If you have an open object with a "write" property, you can do
    print >> thefile, mystring
    and Python will simply redirect the output to "thefile" instead of
    sys.stdout.
    tac-tics, Jun 30, 2006
    #2
    1. Advertising

  3. f = file('output.txt','w')
    print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
    size / count,
    count,
    size)
    f.close()
    > hello,
    >
    > one more question i will have!
    >
    > now i have written a little programm, which delivers me an output on the
    > shell!
    >
    >
    > here is the print command, which delivers me the following output (see
    > below) on the shell:
    >
    >
    > print '%-30s | %-12d | %-12d |%-12d ' % (typename,
    > size / count,
    > count,
    > size)
    >
    >
    >
    > --------------NAME-------------|Groesse(Byte)-|----Anzahl----|-Gesamtgroesse
    > (Byte)-|
    > ----------------------------------------------------------------------------
    > ----------
    > ADD_REAL_N6 | 4 | 1 |4
    > AND_BOOL_N10 | 4 | 1 |4
    > AND_BOOL_N12 | 4 | 1 |4
    >
    >
    >
    > Is there a way to put this output in an file?!?! i searched about 2h for
    > this, but i couldn`t find an answer!
    >
    > thnks, juergen
    >
    Stephan Wassipaul, Jun 30, 2006
    #3
  4. hello,

    if i would type in your code, i became the following output in the
    "output.txt" - file


    BOOL | 4 | 50463 |201852


    but why?!
    he wouldn´t do that for all the entrys in the csv file! but only for the
    first one in the file!




    Stephan Wassipaul wrote:
    > f = file('output.txt','w')
    > print >>f, '%-30s | %-12d | %-12d |%-12d ' % (typename,
    > size / count,
    > count,
    > size)
    > f.close()
    >> hello,
    >>
    >> one more question i will have!
    >>
    >> now i have written a little programm, which delivers me an output on
    >> the shell!
    >>
    >>
    >> here is the print command, which delivers me the following output
    >> (see below) on the shell:
    >>
    >>
    >> print '%-30s | %-12d | %-12d |%-12d ' % (typename,
    >> size / count,
    >> count,
    >> size)
    >>
    >>
    >>
    >> --------------NAME-------------|Groesse(Byte)-|----Anzahl----|-Gesamtgroe

    sse
    >> (Byte)-|
    >> -------------------------------------------------------------------------

    ---
    >> ----------
    >> ADD_REAL_N6 | 4 | 1 |4
    >> AND_BOOL_N10 | 4 | 1 |4
    >> AND_BOOL_N12 | 4 | 1 |4
    >>
    >>
    >>
    >> Is there a way to put this output in an file?!?! i searched about 2h
    >> for this, but i couldn`t find an answer!
    >>
    >> thnks, juergen
    Juergen Huber, Jun 30, 2006
    #4
  5. Juergen Huber

    Juho Schultz Guest

    Juergen Huber wrote:
    > hello,
    >
    > one more question i will have!
    >
    > now i have written a little programm, which delivers me an output on the
    > shell!
    >


    >
    > Is there a way to put this output in an file?!?! i searched about 2h for
    > this, but i couldn`t find an answer!
    >
    > thnks, juergen


    Others have suggested creating a file.

    You could also let the shell to do the work.

    If you run the program "normally", output goes to screen:
    python myprog.py

    Run and redirect the output to a file:
    python myprog.py > output_of_myprog.txt

    This also makes controlling the output filename much easier.

    You could find the following useful:
    http://www.swc.scipy.org/lec/shell01.html
    Juho Schultz, Jun 30, 2006
    #5
  6. Juergen Huber wrote:
    ....
    > here is the print command, which delivers me the following output (see
    > below) on the shell:
    > print '%-30s | %-12d | %-12d |%-12d ' % (typename,
    > size / count,
    > count,
    > size)

    ....
    > Is there a way to put this output in an file?!?!


    Another way nobody has yet mentioned:

    import sys
    old_output, sys.stdout = sys.stdout, open('somefile.txt', 'w')

    try:
    <<<put call to original code here>>>
    finally:
    old_output, sys.stdout = sys.stdout, old_output
    old_output.close()
    print 'Output safely written to:', old_output.name

    --Scott David Daniels
    Scott David Daniels, Jul 1, 2006
    #6
    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. keto
    Replies:
    0
    Views:
    884
  2. Lew
    Replies:
    0
    Views:
    622
  3. David Cournapeau

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

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    330
    David Cournapeau
    Dec 30, 2008
  4. nickname
    Replies:
    7
    Views:
    1,058
    Nobody
    Aug 26, 2009
  5. Salty Dog
    Replies:
    4
    Views:
    776
    Salty Dog
    Feb 28, 2005
Loading...

Share This Page