0

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?

Mike
  • 69
  • 1
  • 3
  • 9
  • 1
    Perhaps try stderr? `result = subprocess.Popen('bzip2 --version', shell=True, stderr=subprocess.PIPE).communicate()[1]` –  Sep 03 '19 at 10:25
  • doh!! seems i tried everything but stderr and changing the last 0 to a 1 that has fixed it you are a life saver you should make this an answer so i can accept it :) – Mike Sep 03 '19 at 11:38

1 Answers1

1

Seems bzip2 writes to stderr instead of to stdout.

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