Discarding STDERR generated during subprocess.popen

Nobody nobody at nowhere.com
Tue Aug 24 04:53:40 CEST 2010


On Mon, 23 Aug 2010 10:38:02 -0700, Leon Derczynski wrote:

> I would like to run an external program, and discard anything written
> to stderr during its execution, capturing only stdout. My code
> currently looks like:
> 
> def blaheta_tag(filename):
>         blaheta_dir = '/home/leon/signal_annotation/parsers/blaheta/'
>         process = subprocess.Popen([blaheta_dir + 'exec/funcTag',
> blaheta_dir + 'data/', filename], cwd=blaheta_dir,
> stdout=subprocess.PIPE)
>         process.wait()
>         return process.communicate()[0]
> 
> This returns stdout, and stderr ends up printing to the console. How
> can I disregard anything sent to stderr such that it doesn't appear on
> the console?

Either:

1. Add "stderr=subprocess.PIPE" to the Popen() call. The communicate()
method will read both stdout and stderr, and you just ignore stderr.

2. Redirect stderr to the null device:

	nul_f = open(os.devnull, 'w')
	process = subprocess.Popen(..., stderr = nul_f)
	nul_f.close()
	return process.communicate()[0]

[os.devnull will be "/dev/null" on Unix, "nul" on Windows.]

BTW: you shouldn't call process.wait() here. The communicate() method will
call the wait() method when it receives EOF.

If you call wait(), and the process tries to write more than a buffer's
worth of output (the exact figure is platform-specific), your script will
deadlock. The child process will block waiting for the script to consume
its output, while the script will block waiting for the child process to
terminate.




More information about the Python-list mailing list