print to screen and file with one print statement

amiwinter at gmail.com amiwinter at gmail.com
Tue Apr 9 09:31:06 EDT 2013


On Thursday, February 13, 2003 2:51:02 PM UTC+2, Mike Müller wrote:
> "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

Hay Mike,

Can you tell me how & where you added the 'flush(self)' & 'sys.stdout.flush()'?


Thanks!
Ami



More information about the Python-list mailing list