Tags: capture, catch, displayed, output, programming, pytho, python, pythons, stdout, suggesting

How to catch pythons STDOUT

On Programmer » Python

7,079 words with 9 Comments; publish: Wed, 02 Jan 2008 20:43:00 GMT; (200124.02, « »)

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.

All Comments

Leave a comment...

  • 9 Comments
    • praveenkumar.117.python.todaysummary.com.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

      #1; Wed, 02 Jan 2008 20:45:00 GMT
    • praveenkumar.117.python.todaysummary.com.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

      #2; Wed, 02 Jan 2008 20:46:00 GMT
    • 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

      #3; Wed, 02 Jan 2008 20:47:00 GMT
    • praveenkumar.117.python.todaysummary.com.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.

      #4; Wed, 02 Jan 2008 20:48:00 GMT
    • praveenkumar.117.python.todaysummary.com.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

      #5; Wed, 02 Jan 2008 20:49:00 GMT
    • praveenkumar.117.python.todaysummary.com.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

      #6; Wed, 02 Jan 2008 20:50:00 GMT
    • 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

      #7; Wed, 02 Jan 2008 20:51:00 GMT
    • 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

      #8; Wed, 02 Jan 2008 20:52:00 GMT
    • praveenkumar.117.python.todaysummary.com.gmail.com wrote:

      Quote:
      === Original Words ===

      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

#9; Wed, 02 Jan 2008 20:53:00 GMT