print shell output in a file

Scott David Daniels scott.daniels at acm.org
Sat Jul 1 13:23:36 EDT 2006


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.daniels at acm.org



More information about the Python-list mailing list