1

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?

  • 4
    Is the application printing the version information to STDERR? Try adding `stderr=subprocess.STDOUT` to your `Popen` call and see what happens. – Sam Mussmann Feb 14 '14 at 20:34
  • Glad to help! I added an answer for better visibility for people who come across this question later. – Sam Mussmann Feb 14 '14 at 20:44
  • note: if you want to discard the output then you should use [`DEVNULL`](http://stackoverflow.com/a/11270665/4279) instead of `PIPE` otherwise you can run out of memory if the child process generates enough output. – jfs Feb 18 '14 at 09:47

1 Answers1

3

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]
Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43