Search code examples
python-2.7subprocessbzip2

Trying to get some output from subprocess.Popen works for all commands but bzip2


I am trying to get the output from subprocess.Popen assign it to a variable and then work with it in the rest of my program however it is executing code without ever assigning it to my variable

my current line of code is result = subprocess.Popen('bzip2 --version', shell=True, stdout=subprocess.PIPE).communicate()[0] currently to test it im just printing the length and the results which are currently empty it does execute the code but it shows up in the terminal prior to my prints

I have tried the above-mentioned code using other commands and it works just as I would expect

any suggestions on how I can go about doing this?


Solution

  • Seems bzip2 writes to stderr instead of to stdout.

    result = subprocess.Popen('bzip2 --version', shell=True, stderr=subprocess.PIPE).communicate()[1]