swingsoneto
swingsoneto

Reputation: 65

How to read output from subprocess without printing to terminal?

I've searched fairly thoroughly for an answer, but haven't found any solutions to this.

I need to execute a child process only momentarily to print its version information. If run from the command line, it's simply

.\bin\application.exe --version

and version information is printed to the terminal. I'd like to capture that information in a string. I've been trying out variants of

args = ("path\to\application.exe", "--version")
subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]

with no success. I also need this not to print the output to the terminal, if possible. What can I do?

Upvotes: 1

Views: 622

Answers (1)

Sam Mussmann
Sam Mussmann

Reputation: 5983

Sometimes applications dump version information to STDERR instead of STDOUT.

You can capture both like this:

args = ("path\to\application.exe", "--version")
subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]

Upvotes: 3

Related Questions