Write both in std output and in a file ?

Steve Holden sholden at holdenweb.com
Wed Jan 16 08:26:07 EST 2002


"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().

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list