[How to change redirect stderr?]
Moretti
moretti at igs.cnrs-mrs.fr
Mon Mar 13 02:43:34 EST 2006
Fredrik Lundh wrote:
> "Moretti" wrote:
>
>> I would like to be able to change the standard error path.
>
> (I'm not sure path is the right word here, really, but never mind...)
>
>> How can I do this in a script if I want to send error messages to
>> /dev/null by example ?
>
> 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.flush()
> err = open('/dev/null', 'a+', 0)
> os.dup2(err.fileno(), sys.stderr.fileno())
>
> hope this helps!
>
> </F>
Thanks
It's exactly what I seek for.
--
Sebastien Moretti
More information about the Python-list
mailing list