check_output error in python

misguided picture misguided · Jul 9, 2013 · Viewed 15.8k times · Source

I am gettin a error while running the below code.

#!/usr/bin/python
import subprocess
import os
def check_output(*popenargs, **kwargs):
    process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        error = subprocess.CalledProcessError(retcode, cmd)
        error.output = output
        raise error
    return output

location = "%s/folder"%(os.environ["Home"])
subprocess.check_output(['./MyFile'])

Error

subprocess.check_output(['./MyFile'])
AttributeError: 'module' object has no attribute 'check_output'

I am working on Python 2.6.4 .

Answer

Travis DePrato picture Travis DePrato · Jul 9, 2013

You probably just want to use check_output, but, just so you know, there is a method subprocess.check_output, but it's not defined until Python 2.7 (http://docs.python.org/3/library/subprocess.html#subprocess.check_output)

You might even want this, which defines the function in the module if it's not there (i.e. running before v2.7).

try: subprocess.check_output
except: subprocess.check_output = check_output
subprocess.check_output()