How to send output to file

Just just at xs4all.nl
Fri Jan 24 04:16:16 EST 2003


In article <mailman.1043398544.26300.python-list at python.org>,
 Gerrit Holl <gerrit at nl.linux.org> wrote:

> A schreef op dinsdag 21 januari om 20:27:56 +0000:
> > Dear Gerrit,
> > Thank you for your email.
> > Now I already know how I can assing the output to a file.
> > Is there a possibility  to assign the output back to standard as it was 
> > before I assigned to a 
> > file?
> 
> You can use sys.__stdout__ for that: sys.stdout = sys.__stdout__

Yet as a matter of principle it's better to store the original(s) and 
stuff them back when you're done in a finally clause, eg:

   so, se = sys.stdout, sys.stderr
   sys.stdout = sys.stderr = myfile
   try:
      ..do stuff..
   finally:
      sys.stdout, sys.stderr = so, se

After all it's better not to assume sys.stdout is sys.__stdout__ at the 
time your code is run.

Just




More information about the Python-list mailing list