Discarding STDERR generated during subprocess.popen

Thomas Jollans thomas at jollybox.de
Mon Aug 23 13:53:40 EDT 2010


On Monday 23 August 2010, it occurred to Leon Derczynski to exclaim:
> 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.



More information about the Python-list mailing list