How to catch python's STDOUT

Discussion in 'Python' started by praveenkumar.117@gmail.com, Apr 6, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
    Sybren Stuvel, Apr 6, 2006
    #2
    1. Advertising

  3. 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>
    Fredrik Lundh, Apr 6, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. 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.
    Steven D'Aprano, Apr 6, 2006
    #5
  6. 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>
    Fredrik Lundh, Apr 6, 2006
    #6
  7. 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
    Sybren Stuvel, Apr 6, 2006
    #7
  8. 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
    Sybren Stuvel, Apr 6, 2006
    #8
  9. Peter Hansen Guest

    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
    Peter Hansen, Apr 6, 2006
    #9
  10. Greg Ewing Guest

    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
    Greg Ewing, Aug 1, 2006
    #10
  11. stone_nomad

    Joined:
    Oct 19, 2009
    Messages:
    1
    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"))
    
    stone_nomad, Oct 19, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    401
  2. Replies:
    4
    Views:
    436
    Marc 'BlackJack' Rintsch
    Nov 7, 2007
  3. Andreas S
    Replies:
    3
    Views:
    245
    Eric Hodel
    Dec 9, 2006
  4. Petterson Mikael

    Catch stdout from eval possible

    Petterson Mikael, Jan 20, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    152
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page