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
.
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.
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).
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.
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.
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()
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'])
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