Write both in std output and in a file ?

Barghest barghest at wanadoo.fr
Wed Jan 16 15:36:45 CET 2002


OK thanks

"Steve Holden" <sholden at holdenweb.com> a écrit dans le message news:
aef18.41801$Ot.2014111 at atlpnn01.usenetserver.com...
> "Daniel Dittmar" <daniel.dittmar at sap.com> wrote ...
> > > Is it possible to print output both in the console and in a file ?
> >
> > class TeeStream:
> >     def __init__ (self, *streams):
> >         self.streams = streams
> >
> >     def write (self, data):
> >         for stream in self.streams:
> >             stream.write (data)
> >
> > call as
> > stream = TeeStream (sys.stdout, open ('...', 'wt'))
> >
> ... or as
>
>     sys.stdout = TeeStream(sys.stdout, open('myfile.txt', "w")
>
> in which case print should work as standard, whearas the original example
> requires
>
>     print >> stream, things
>
> BTW, was TeeStream (sys.stdout, open ('...', 'wt')) a typo? Don't see any
> 't' mode character in the docs for file()/open().






More information about the Python-list mailing list