I've been reading the Python documentation about the subprocess module (see here) and it talks about a subprocess.check_output()
command which seems to be exactly what I need.
However, when I try and use it I get an error that it doesn't exist, and when I run dir(subprocess)
it is not listed.
I am running Python 2.6.5, and the code I have used is below:
import subprocess subprocess.check_output(["ls", "-l", "/dev/null"])
Does anyone have any idea why this is happening?
The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.
CalledProcessError Exception raised when a process run by check_call() or check_output() returns a non-zero exit status. returncode Exit status of the child process.
check_call will raise an exception if the command it's running exits with anything other than 0 as its status.
Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run.
It was introduced in 2.7 See the docs.
Use subprocess.Popen if you want the output:
>>> import subprocess >>> output = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE).communicate()[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With