Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.check_output() doesn't seem to exist (Python 2.6.5)

Tags:

python

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?

like image 260
robintw Avatar asked Jan 27 '11 10:01

robintw


People also ask

What is subprocess Check_output in Python?

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.

What is the return type of subprocess Check_output?

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.

Does subprocess call raise exception?

check_call will raise an exception if the command it's running exits with anything other than 0 as its status.

Why are shells true in subprocess?

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.


1 Answers

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] 
like image 199
user225312 Avatar answered Oct 13 '22 04:10

user225312