How to send output to file

Mongryong Mongryong at sympatico.ca
Thu Jan 23 19:12:29 EST 2003


If you're just debugging, you might want to override sys.stderr instead
of sys.stdout (ie sys.stderr = open("log","w")).  Use sys.stdout for
normal program output.

Alternatively, you can use your own global variable (ie. MYLOG =
open("log", "w")).

If you're using the 'print "some string"' convention, don't forget to
add your output handler to the print command:
ie.  1. print >> sys.stderr, "string..."
ie   2. print >> MYLOG, "string...."

My advice would be to stay awhile from the "print" convention for
outputting text.

On Thu, 2003-01-23 at 18:14, Stefan Schwarzer wrote:
> Gerrit Holl wrote:
> > A schreef op dinsdag 21 januari om 09:54:37 +0000:
> >> I use httplib module.
> >> By h.set_debuglevel(1) I start debugging but the output is written to DOS window.
> >> How can  I divert the output to file instead.
> >> Thank you for help.
> > 
> > You can assing to sys.stdout
> > 
> > import sys
> > sys.stdout = open("myfile.log", "w")
> 
> Specifically, you can set and reset sys.stdout:
> 
> sys.stdout = open("myfile.log", "w")  # like above
> try:
>      # do things that write to sys.stdout
> finally:
>      # sys.__stdout__ contains the "real" standard output
>      sys.stdout = sys.__stdout__
> 
> Note that the above approach will also affect print statements in other threads
> than the one which should use the redirection.
> 
> Stefan
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list







More information about the Python-list mailing list