Popen3 and capturestderr

Donn Cave donn at drizzle.com
Wed Mar 9 07:17:50 CET 2005


Quoth Kenneth Pronovici <pronovic at skyjammer.com>:
...
| If ignoreStderr=False, I use popen2.Popen4 so that stderr and stdout are
| intermingled.  If ignoreStderr=True, I use popen2.Popen3 with
| capturestderr=True so stderr doesn't appear in the output.  This
| functionality exists so I have an equivalent of command-line redirection
| of stderr, i.e. command 2>/dev/null.  
...
| After some digging, I've decided that this behavior probably occurs
| because I am ignoring the pipe.childerr file object.  Indeed, if I call
| pipe.childerr.close() right after opening the pipe, my "ls" command that
| had been hanging completes normally.  However, other commands which
| actually attempt to write to stderr don't seem to like this very much.
|
| What is the right way to discard stderr when working with a pipe?  I
| want to consistently throw it away, and I don't see a good way to do
| this with the popen2 implementation.

Right, popen2 gives you about 3 options, out of probably dozens that
you could get with shell redirections.  On the other hand, the source
is available, and Python is an OOP language, so I assume there is no
reason you can't make a derived class that does just what you want.
In the present case I guess that would mean something like
   null = os.open('/dev/null', os.O_RDWR)
   os.dup2(null, 0)
   os.dup2(null, 2) (depending)
   os.close(null)
along with other stuff you can just copy from Popen4.

	Donn



More information about the Python-list mailing list