print to screen and file with one print statement
Mike Müller
mmueller at dgfz.de
Thu Feb 13 07:51:02 EST 2003
"Mark McEahern" <marklists at mceahern.com> wrote in message news:<mailman.1045085143.24238.python-list at python.org>...
> Same basic idea:
>
> #!/usr/bin/env python
>
> import sys
>
> class MyWriter:
>
> def __init__(self, stdout, filename):
> self.stdout = stdout
> self.logfile = file(filename, 'a')
>
> def write(self, text):
> self.stdout.write(text)
> self.logfile.write(text)
>
> def close(self):
> self.stdout.close()
> self.logfile.close()
>
> writer = MyWriter(sys.stdout, 'log.txt')
> sys.stdout = writer
>
> print 'test'
>
Hi Mark,
works perfect. Just had to change ´file´ to ´open´ for my Python 2.1.
Adding the method ´flush(self)´ to Writer helped to get my
´sys.stdout.flush()´ to work.
Thanks also to two inches (previous post).
Mike
More information about the Python-list
mailing list