redirecting stdout to a file as well as screen

SamG mad.vijay at gmail.com
Thu Apr 12 11:01:18 CEST 2007


On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi... at gmail.com> escribió:
>
> > How could i make, from inside the program, to have the stdout and
> > stderr to be printed both to a file as well the terminal(as usual).
>
> A very minimal example:
>
> import sys
>
> class Tee(file):
>      others = ()
>
>      def write(self, data):
>          file.write(self, data)
>          for f in others:
>              f.write(data)
>
> tee = Tee(r"c:\temp\output.log","wt")
> tee.others = [sys.stdout, sys.stderr]
> sys.stdout = sys.stderr = tee
>
> print dir(sys)
> sys.foo # error
>
> --
> Gabriel Genellina

This is only creating an out.log file and all the stdout and stderr
are logged there.




More information about the Python-list mailing list