[How to change redirect stderr?]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Moretti

    #1

    [How to change redirect stderr?]

    Hello,
    I would like to be able to change the standard error path.

    How can I do this in a script if I want to send error messages to /dev/null
    by example ?

    Thanks

    --
    Sebastien Moretti
  • JuHui

    #2
    Re: [How to change redirect stderr?]

    how about
    $ python test.py 2> /dev/null
    ?

    Comment

    • Fredrik Lundh

      #3
      Re: [How to change redirect stderr?]

      "Moretti" wrote:
      [color=blue]
      > I would like to be able to change the standard error path.[/color]

      (I'm not sure path is the right word here, really, but never mind...)
      [color=blue]
      > How can I do this in a script if I want to send error messages to /dev/null
      > by example ?[/color]

      if you're talking about things that Python prints to stderr, all you need
      to do is to replace sys.stderr with something more suitable:

      import sys
      sys.stderr = open("/dev/null", "w")

      or, more portable:

      class NullDevice:
      def write(self, s):
      pass

      sys.stderr = NullDevice()

      if you want to redirect both things printed via sys.stderr and things
      printed to stderr at the C level, you need to redirect the STDERR file
      handle. here's one way to do that:

      import os, sys

      sys.stderr.flus h()
      err = open('/dev/null', 'a+', 0)
      os.dup2(err.fil eno(), sys.stderr.file no())

      hope this helps!

      </F>



      Comment

      • Moretti

        #4
        Re: [How to change redirect stderr?]

        Fredrik Lundh wrote:
        [color=blue]
        > "Moretti" wrote:
        >[color=green]
        >> I would like to be able to change the standard error path.[/color]
        >
        > (I'm not sure path is the right word here, really, but never mind...)
        >[color=green]
        >> How can I do this in a script if I want to send error messages to
        >> /dev/null by example ?[/color]
        >
        > if you're talking about things that Python prints to stderr, all you need
        > to do is to replace sys.stderr with something more suitable:
        >
        > import sys
        > sys.stderr = open("/dev/null", "w")
        >
        > or, more portable:
        >
        > class NullDevice:
        > def write(self, s):
        > pass
        >
        > sys.stderr = NullDevice()
        >
        > if you want to redirect both things printed via sys.stderr and things
        > printed to stderr at the C level, you need to redirect the STDERR file
        > handle. here's one way to do that:
        >
        > import os, sys
        >
        > sys.stderr.flus h()
        > err = open('/dev/null', 'a+', 0)
        > os.dup2(err.fil eno(), sys.stderr.file no())
        >
        > hope this helps!
        >
        > </F>[/color]

        Thanks
        It's exactly what I seek for.

        --
        Sebastien Moretti

        Comment

        Working...