How to catch python's STDOUT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • praveenkumar.117@gmail.com

    #1

    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.

  • Sybren Stuvel

    #2
    Re: How to catch python's STDOUT

    praveenkumar.11 7@gmail.com enlightened us with:[color=blue]
    > 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.[/color]

    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

    Comment

    • Fredrik Lundh

      #3
      Re: How to catch python's STDOUT

      praveenkumar.11 7@gmail.com wrote:
      [color=blue]
      > 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.[/color]

      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>



      Comment

      • praveenkumar.117@gmail.com

        #4
        Re: How to catch python's STDOUT

        HI,
        I am a member of comp.lang.pytho n.
        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

        Comment

        • Steven D'Aprano

          #5
          Re: How to catch python's STDOUT

          praveenkumar.11 7@gmail.com wrote:
          [color=blue]
          > 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.[/color]


          [color=blue][color=green][color=darkred]
          >>> import sys, StringIO
          >>> SAVEOUT = sys.stdout
          >>> capture = StringIO.String IO()
          >>> sys.stdout = capture
          >>> print "hello"
          >>>[/color][/color][/color]

          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.String IO()
          _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.

          Comment

          • Fredrik Lundh

            #6
            Re: How to catch python's STDOUT

            praveenkumar.11 7@gmail.com wrote:
            [color=blue]
            > 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[/color]

            you can print directly to a log file:

            mylogfile = open("logfile.t xt", "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.t xt", "a")

            or you can use the "logging" module:

            Source code: Lib/logging/__init__.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor...


            etc.

            hope this helps!

            </F>



            Comment

            • Sybren Stuvel

              #7
              Re: How to catch python's STDOUT

              praveenkumar.11 7@gmail.com enlightened us with:[color=blue]
              > 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[/color]

              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

              Comment

              • Sybren Stuvel

                #8
                Re: How to catch python's STDOUT

                Fredrik Lundh enlightened us with:[color=blue]
                > or you can use the "logging" module:
                >
                > http://docs.python.org/lib/module-logging.html[/color]

                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

                Comment

                • Peter Hansen

                  #9
                  Re: How to catch python's STDOUT

                  Steven D'Aprano wrote:[color=blue][color=green][color=darkred]
                  > >>> import sys, StringIO
                  > >>> SAVEOUT = sys.stdout
                  > >>> capture = StringIO.String IO()
                  > >>> sys.stdout = capture
                  > >>> print "hello"
                  > >>>[/color][/color]
                  >
                  > But be warned, I've had difficulty restoring stdout
                  > afterwards, and needed to exit the interactive
                  > interpreter to get things back to normal.[/color]

                  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

                  Comment

                  • Greg Ewing

                    #10
                    Re: How to catch python's STDOUT

                    praveenkumar.11 7@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_debugg ing_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

                    Comment

                    Working...