Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60221 > unrolled thread
| Started by | Himanshu Garg <hgarg.india@gmail.com> |
|---|---|
| First post | 2013-11-22 05:51 -0800 |
| Last post | 2013-11-23 23:00 -0800 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Himanshu Garg <hgarg.india@gmail.com> |
|---|---|
| Date | 2013-11-22 05:51 -0800 |
| Subject | Help me to print to screen as well as log |
| Message-ID | <08d39aeb-cc25-4a80-b42e-fd8eac9a9d1a@googlegroups.com> |
I want that print "hello" should appear on screen as well as get saved in a log file. How can I accomplish this?
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-22 14:15 +0000 |
| Message-ID | <528f66ec$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #60221 |
On Fri, 22 Nov 2013 05:51:21 -0800, Himanshu Garg wrote:
> I want that print "hello" should appear on screen as well as get saved
> in a log file.
>
> How can I accomplish this?
print "hello"
logfile.write("hello\n")
Does that satisfy your need? If not, please explain in more detail what
you are trying to do, what you have already tried, and what happened when
you tried it.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-22 15:38 +0000 |
| Message-ID | <mailman.3046.1385134756.18130.python-list@python.org> |
| In reply to | #60225 |
On 22/11/2013 14:15, Steven D'Aprano wrote:
> On Fri, 22 Nov 2013 05:51:21 -0800, Himanshu Garg wrote:
>
>> I want that print "hello" should appear on screen as well as get saved
>> in a log file.
>>
>> How can I accomplish this?
>
> print "hello"
> logfile.write("hello\n")
>
>
> Does that satisfy your need? If not, please explain in more detail what
> you are trying to do, what you have already tried, and what happened when
> you tried it.
>
Fancy wasting vertical space like that, if you use a semi-colon you can
save a whole lot of it like this
print "hello";logfile.write("hello\n") # :)
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-11-22 15:26 +0100 |
| Message-ID | <mailman.3040.1385130369.18130.python-list@python.org> |
| In reply to | #60221 |
Himanshu Garg wrote: > I want that print "hello" should appear on screen as well as get saved in > a log file. > > How can I accomplish this? In Python 3 print() is a function -- you can replace it with a custom function that invokes the original print() twice. In both Python 3 and Python 2 you can redirect sys.stdout/stderr to a custom object with a write() method. If you are actually logging I suggest that you look into the logging package which allows multiple handlers, see http://docs.python.org/3.3/howto/logging.html You will need to replace your print() calls with some_logger.info(message) or similar.
[toc] | [prev] | [next] | [standalone]
| From | Himanshu Garg <hgarg.india@gmail.com> |
|---|---|
| Date | 2013-11-23 05:11 -0800 |
| Message-ID | <a0f52e41-7833-4320-b749-023468127335@googlegroups.com> |
| In reply to | #60221 |
How can I write to the same file from two different scripts opened at same time?
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-11-23 18:02 -0500 |
| Message-ID | <mailman.3108.1385247704.18130.python-list@python.org> |
| In reply to | #60295 |
On Sat, 23 Nov 2013 05:11:11 -0800 (PST), Himanshu Garg <hgarg.india@gmail.com> wrote: > How can I write to the same file from two different scripts opened at same time? Using what version of python and on what OS? Sone OS's will open the file exclusively by default. Others will let you stomp all over some other process' output. Why not show us what you're trying and what happens and ask a much more specific question? -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2013-11-23 16:23 -0800 |
| Message-ID | <b844ea61-b9b8-4a36-a93b-e136715db006@googlegroups.com> |
| In reply to | #60221 |
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Himanshu Garg <hgarg.india@gmail.com> |
|---|---|
| Date | 2013-11-23 23:00 -0800 |
| Message-ID | <f660a9fa-4883-4190-bcdc-4bf29841d092@googlegroups.com> |
| In reply to | #60221 |
I have simply opened file in append mode in linux.
script 1 :
file = open("output.log", "a")
file.write()
file.flush()
script 2:
file = open("output.log", "a")
file.write()
file.flush()
It writes properly to the file.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web