Discarding STDERR generated during subprocess.popen

L

Leon Derczynski

Hi,

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?

Thanks
 
T

Thomas Jollans

Hi,

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?

Read it into a pipe as well, and then ignore. Or, if you're targeting only
UNIX-like systems, open /dev/null for writing and redirect the output there.
 
N

Nobody

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top