Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to catch python's STDOUT

Reply
Thread Tools

How to catch python's STDOUT

 
 
praveenkumar.117@gmail.com
Guest
Posts: n/a
 
      04-06-2006
Hi,
Can someone help me by suggesting how to capture python's
STDOUT. I doesn't want the python's output to get displayed on the
screen.

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      04-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with:
> Can someone help me by suggesting how to capture python's STDOUT. I
> doesn't want the python's output to get displayed on the screen.


python somescript.py > /dev/null

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-06-2006
(E-Mail Removed) wrote:

> Can someone help me by suggesting how to capture python's
> STDOUT. I doesn't want the python's output to get displayed on the
> screen.


you can replace sys.stdout (and/or sys.stderr) with your own file-like
object, e.g.

class NullStream:
def write(self, text):
pass

import sys
sys.stdout = NullStream()

if this doesn't solve your problem, you have to be a bit more specific
(since in general, python doesn't print anything unless you ask it to...)

hope this helps!

</F>



 
Reply With Quote
 
praveenkumar.117@gmail.com
Guest
Posts: n/a
 
      04-06-2006
HI,
I am a member of comp.lang.python.
I posted a message saying how to capture python's STDOUT.
sorry i did not clearly mentioned the problem.

I have python script in which i have some print statements.
I dont want the outputs of print to be displayed on the console
since it is used my fellow-workers
But i need those prints for debugging purpose
So some how i want to capture those prints
can u please suggest
regards
praveen kumar

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-06-2006
(E-Mail Removed) wrote:

> Hi,
> Can someone help me by suggesting how to capture python's
> STDOUT. I doesn't want the python's output to get displayed on the
> screen.




>>> import sys, StringIO
>>> SAVEOUT = sys.stdout
>>> capture = StringIO.StringIO()
>>> sys.stdout = capture
>>> print "hello"
>>>


But be warned, I've had difficulty restoring stdout
afterwards, and needed to exit the interactive
interpreter to get things back to normal.

Because I'm paranoid, I might prefer to leave
sys.stdout as it, and use a custom print
function which I control:

_CAPTURE = StringIO.StringIO()
_FLAG = True # start capturing

def print(obj):
global _CAPTURE, _FLAG
if _FLAG:
where = _CAPTURE
else:
where = sys.stdout
print >>where, obj

Then I can start or stop capturing printing just by
changing a flag.


--
Steven.

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-06-2006
(E-Mail Removed) wrote:

> I have python script in which i have some print statements.
> I dont want the outputs of print to be displayed on the console
> since it is used my fellow-workers
> But i need those prints for debugging purpose
> So some how i want to capture those prints
> can u please suggest


you can print directly to a log file:

mylogfile = open("logfile.txt", "a")

print >>mylogfile, "my log message"

or you can replace sys.stdout (and/or sys.stderr) with the log file object:

import sys
sys.stdout = sys.stderr = open("logfile.txt", "a")

or you can use the "logging" module:

http://docs.python.org/lib/module-logging.html

etc.

hope this helps!

</F>



 
Reply With Quote
 
Sybren Stuvel
Guest
Posts: n/a
 
      04-06-2006
(E-Mail Removed) enlightened us with:
> I have python script in which i have some print statements. I dont
> want the outputs of print to be displayed on the console since it is
> used my fellow-workers But i need those prints for debugging purpose
> So some how i want to capture those prints can u please suggest


I suggest you remove the print statements and start using the logging
module instead. It's much more powerful, and allows you to put debug
statements in a file, for instance.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
 
Reply With Quote
 
Sybren Stuvel
Guest
Posts: n/a
 
      04-06-2006
Fredrik Lundh enlightened us with:
> or you can use the "logging" module:
>
> http://docs.python.org/lib/module-logging.html


I'd definitely do that.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-06-2006
Steven D'Aprano wrote:
> >>> import sys, StringIO
> >>> SAVEOUT = sys.stdout
> >>> capture = StringIO.StringIO()
> >>> sys.stdout = capture
> >>> print "hello"
> >>>

>
> But be warned, I've had difficulty restoring stdout
> afterwards, and needed to exit the interactive
> interpreter to get things back to normal.


If you had difficulty, perhaps knowing about sys.__stdout__ would have
helped... (?) There's no need to preserve the original sys.stdout as
you do above (in simple scripts, anyway) since Python does it for you.
(Yes, in a library routine such as unittest you might need to do it in
case the calling code has already modified it, but I doubt that's
relevant in the OP's case.)

-Peter

 
Reply With Quote
 
Greg Ewing
Guest
Posts: n/a
 
      08-01-2006
(E-Mail Removed) wrote:

> I dont want the outputs of print to be displayed on the console
> since it is used my fellow-workers
> But i need those prints for debugging purpose


import sys
sys.stdout = open("my_debugging_output.txt", "w")

Or you can replace sys.stdout with any object having
a write() method which does whatever you want with
the output.

You can similarly replace sys.stderr to capture
output written to standard error.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
os.popen does not seem to catch stdout zane.selvans@gmail.com Python 4 11-07-2007 07:42 PM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM



Advertisments