Search code examples
pythonpython-2.6

check_output error in python


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 .


Solution

  • Just use :

    check_output(['./MyFile'])
    

    You've defined your own function, it's not an attribute of subprocess module(for Python 2.6 and earlier).

    You can also assign the function to the imported module object(but that's not necessary):

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