473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to catch python's STDOUT

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.

Apr 6 '06 #1
9 3932
pr**************@gmail.com 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
Apr 6 '06 #2
pr**************@gmail.com 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>

Apr 6 '06 #3
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

Apr 6 '06 #4
pr**************@gmail.com 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.

Apr 6 '06 #5
pr**************@gmail.com 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>

Apr 6 '06 #6
pr**************@gmail.com 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
Apr 6 '06 #7
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
Apr 6 '06 #8
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

Apr 6 '06 #9
pr**************@gmail.com 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
Aug 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Edward Diener | last post by:
From within a function in my own module I need to take the output from a Python module "A", which is sending data to stdout and which can not be changed and which I need to invoke as a top-level...
6
by: Farshid Lashkari | last post by:
Hi, My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
10
by: A.M | last post by:
Hi, I am having difficulty with shell scripting in Python. I use the following command to run a DOS command and put the return value in a Python variable:
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 413 open ( +1) / 3407 closed (+10) / 3820 total (+11) Bugs : 897 open ( -3) / 6167 closed (+18) / 7064 total (+15) RFE : 234 open...
2
by: Pierre Rouleau | last post by:
Hi all, I have a consistent test case where os.popen3() hangs in Windows. The system hangs when retrieving the lines from the child process stdout. I know there were several reports related to...
4
by: zane.selvans | last post by:
Hi there, I've been banging my head against this for a day, and I can't take it anymore. It's probably a stupid error, but I don't see where. I'm trying to use Python to call an external...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.