Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 530
misguided Avatar asked Jul 09 '13 04:07

misguided


People also ask

What is Check_output?

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.

How can we avoid shell true in subprocess?

From the docs: args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).

What is Popen Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

What is Popen?

General description. The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from or write to the pipe.


2 Answers

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()
like image 163
Travis DePrato Avatar answered Sep 18 '22 12:09

Travis DePrato


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'])
like image 33
Ashwini Chaudhary Avatar answered Sep 18 '22 12:09

Ashwini Chaudhary