Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

print to screen and file with one print statement

6,483 views
Skip to first unread message

Mike Müller

unread,
Feb 12, 2003, 3:46:32 PM2/12/03
to
I would like to send my print statements to the terminal and to a log
file at the same time. I can use redirect as describe in Dive into
Python:

import sys

print 'Dive in'
saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
print 'This message will be logged but not displayed'
print 'This message should be logged and also displayed'
sys.stdout = saveout
fsock.close()

This redirects all output from Python to the open file.
At the same time I'd like to see all printed text on screen.
How can I do this?

Thanks

Mike

Two Inches

unread,
Feb 12, 2003, 4:34:46 PM2/12/03
to
How about this:

class writer :
def __init__(self, *writers) :
self.writers = writers

def write(self, text) :
for w in self.writers :
w.write(text)

import sys

saved = sys.stdout
fout = file('out.log', 'w')
sys.stdout = writer(sys.stdout, fout)
print "There you go."
sys.stdout = saved
fout.close()

Mark McEahern

unread,
Feb 12, 2003, 4:24:06 PM2/12/03
to
[Mike Müller]

> This redirects all output from Python to the open file.
> At the same time I'd like to see all printed text on screen.
> How can I do this?

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'

Cheers,

// m

-


Mike Müller

unread,
Feb 13, 2003, 7:51:02 AM2/13/03
to
"Mark McEahern" <mark...@mceahern.com> wrote in message news:<mailman.1045085143...@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

amiw...@gmail.com

unread,
Apr 9, 2013, 9:31:06 AM4/9/13
to
Hay Mike,

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


Thanks!
Ami

KS

unread,
Aug 27, 2014, 6:23:00 PM8/27/14
to
Hi Mike/Ami,

Can you please let me know where do I add flush(self) and get sys.stdout.flush() to work?

Thanks,
KS

Chris Angelico

unread,
Aug 27, 2014, 6:49:04 PM8/27/14
to pytho...@python.org
On Thu, Aug 28, 2014 at 8:23 AM, KS <sen.ku...@gmail.com> wrote:
> Can you please let me know where do I add flush(self) and get sys.stdout.flush() to work?

Check the dates. You're responding to a 2013 response to a 2003 post.
If you have a question about something this old, it's probably best to
start a new thread.

Also, please avoid Google Groups, unless you're prepared to fix its
problems... like the horribly obvious one here that you haven't quoted
even an iota from the original text.

ChrisA
0 new messages