scj2012
September 18, 2019, 10:07am
1
So I have this basic script, see below
import subprocess
import shlex
command = "gcloud projects list"
subprocess.check_output(shlex.split(command))
subprocess.check_call(shlex.split(command))
The subprocess.check_call(shlex.split(command))
actually return what I expect. It returns the result of gcloud
projects list. However subprocess.check_output
doesn't, in fact it returns nothing.
Can explain this?
My understanding is the operation of the two are different:
Documentation -
check_call() returns as soon as /bin/sh process exits, it does not wait for child processes to complete
check_output() waits until all output is read. When (and if) check_output inherits the stdin pipe then check_output() will wait until it exits, i.e., until the child process the inherited pipe. In other words it waits for the child process to end.
Try getting get rid of the shlex,
subprocess.check_output( [ "command" "parameter_1" ])
Yoda
September 18, 2019, 10:56am
3
It is also possible that the command that you are running is sending the output to stderr
instead of stdout
Try this instead:-
subprocess.check_output(shlex.split(command), stderr=subprocess.STDOUT)
scj2012
September 18, 2019, 10:59am
4
My understanding is the operation of the two are different:
Documentation -
Try getting get rid of the shlex,
subprocess.check_output( [ "command" "parameter_1" ])
That's not going to work. I need the shlex and even if I didn't have it, it still wouldn't work.
scj2012
September 18, 2019, 12:13pm
5
yoda:
It is also possible that the command that you are running is sending the output to stderr
instead of stdout
Try this instead:-
subprocess.check_output(shlex.split(command), stderr=subprocess.STDOUT)
That doesn't work etiher. Also the exact same command works with subprocess.check_call. What's weird is the subprocess.check_output works in the python shell but not from the python script.
Yoda
September 18, 2019, 1:11pm
6
To see the output from program, you have to print
the output. I believe stdout
is used internally for this function:-
print subprocess.check_output(shlex.split(command))
1 Like
scj2012
September 18, 2019, 1:24pm
7
yoda:
To see the output from program, you have to print
the output. I believe stdout
is used internally for this function:-
print subprocess.check_output(shlex.split(command))
That does work. Why does it work from the python shell without print?
Also I looked at 17.1. subprocess � Subprocess management � Python 2.7.16 documentation and it doesn't mention anything about having to do a print.