Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60341
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-11-23 16:23 -0800 |
| References | <08d39aeb-cc25-4a80-b42e-fd8eac9a9d1a@googlegroups.com> |
| Message-ID | <b844ea61-b9b8-4a36-a93b-e136715db006@googlegroups.com> (permalink) |
| Subject | Re: Help me to print to screen as well as log |
| From | Miki Tebeka <miki.tebeka@gmail.com> |
> I want that print "hello" should appear on screen as well as get saved in a log file.
> How can I accomplish this?
There are many ways to do this, here's one:
class MultiWriter(object):
def __init__(self, *writers):
self.writers = writers
self.isatty = False
def write(self, data):
for writer in self.writers:
writer.write(data)
def flush(self):
for writer in self.writers:
writer.flush()
out = open('/tmp/log.txt', 'a')
import sys
sys.stdout = MultiWriter(sys.stdout, out)
print('hello')
IMO you're better with the logging package, see http://docs.python.org/2/howto/logging.html#configuring-logging for more details.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-22 05:51 -0800
Re: Help me to print to screen as well as log Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 14:15 +0000
Re: Help me to print to screen as well as log Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-22 15:38 +0000
Re: Help me to print to screen as well as log Peter Otten <__peter__@web.de> - 2013-11-22 15:26 +0100
Re: Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-23 05:11 -0800
Re: Help me to print to screen as well as log Dave Angel <davea@davea.name> - 2013-11-23 18:02 -0500
Re: Help me to print to screen as well as log Miki Tebeka <miki.tebeka@gmail.com> - 2013-11-23 16:23 -0800
Re: Help me to print to screen as well as log Himanshu Garg <hgarg.india@gmail.com> - 2013-11-23 23:00 -0800
csiph-web