How to catch python's STDOUT

  • Thread starter praveenkumar.117
  • Start date
P

praveenkumar.117

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.
 
S

Sybren Stuvel

(e-mail address 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
 
F

Fredrik Lundh

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>
 
P

praveenkumar.117

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
 
S

Steven D'Aprano

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.

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.
 
F

Fredrik Lundh

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>
 
S

Sybren Stuvel

(e-mail address 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
 
P

Peter Hansen

Steven said:
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
 
G

Greg Ewing

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.
 
Joined
Oct 19, 2009
Messages
1
Reaction score
0
some posible code.

Thought I would add this to see what people think. I was looking for a way to test a functions behavior related to printing to stdout. My goal is to use this with unittest. I ran across this thread and it helped me figure out how to do it. I'm curious if anyone thinks this is a bad Idea or have any suggestions for improvement.


Code:
import sys

class Capture(object):
	output = ""
	def write(self, output):
		self.output += output

def testFunctionSTDOUT(function, exspectedvalue):
	c = Capture()
	sys.stdout = c
	function()
	sys.stdout = sys.__stdout__
	if c.output == exspectedvalue:
		return True
	else:
		return False

def testFunctionSTDOUT2(function, functionArguments={}, exspectedvalue=""):
	c = Capture()
	sys.stdout = c
	function(**functionArguments)
	sys.stdout = sys.__stdout__
	if c.output == exspectedvalue:
		return True
	else:
		return False



#test to see if it works
def testfunction():
	print ("text to capture")

def testfunction2(stuff):
	print (stuff)

print(testFunctionSTDOUT(testfunction, "text to capture\n"))
print(testFunctionSTDOUT(testfunction, "this should fail\n"))

print(testFunctionSTDOUT2(testfunction2, {"stuff":"text to capture"} , "text to capture\n"))
print(testFunctionSTDOUT2(testfunction2, {"stuff":"text to capture"} , "this should fail\n"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top