0

i'm trying to capture all output when running a python application using subprocess. i've tried several variants using both subprocess.run and subprocess.Popen. The python app that runs, executes a perl script and this output is captured.

import subprocess as sp

print("some data")
print("some data")

x = subprocess.run(['script.py', 'some', 'options'], stdout=sp.PIPE, stderr=sp.PIPE)

proc_out = sp.stdout
proc_err = sp.stderr

I've also tried adding '> out 2>&1' to the list, tried with capture_output=True, tried redirecting stdout/stderr. The odd thing is that the print statements I'm trying to capture no longer display.

so, it's a python app (which output is captured), that uses subprocess to call another python app (unable to capture it's output), which in turn calls a perl function (which output is captured).

I've been through most of the threads that referenced capturing all data, but still no luck.

Any ideas?

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
echoecho
  • 1
  • 1
  • [My question](https://stackoverflow.com/questions/53965917/streaming-read-from-subprocess) is overkill, but may provide a new starting point for you. – Prune Apr 11 '20 at 21:22
  • Thank you, it looks like my problem is resolved by adding python as the first element of the list. subprocess.run(['python', cmd, args...) Thank you. – echoecho Apr 11 '20 at 23:26

1 Answers1

0
import subprocess
command = "your command"
proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()

You need .communicate(), which writes input then reads all output and waits for the subprocess to exit before continuing execution in current/main thread

sgX
  • 696
  • 1
  • 8
  • 16