10

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 .

2
  • Do you mean to use the check_output method that's defined? Commented Jul 9, 2013 at 4:38
  • There's a very nice way to do this, including detecting if it's necessary, given in this answer. Commented Feb 17, 2015 at 22:50

3 Answers 3

7

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()
Sign up to request clarification or add additional context in comments.

1 Comment

A nicer way to do that is given in this answer.
6

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

3 Comments

File "DailyCheck.py", line 19, in <module> check_output(['./MyFile']) File "DailyCheck.py", line 5, in check_output process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) File "/usr/lib/python2.6/subprocess.py", line 621, in __init__ errread, errwrite) File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory NOt sure why I am getting this error. The file is definitelythere in the folder specified.
Try checking the value of os.getcwd() is correct (should be the directory that MyFile is in).
@TravisGD you are correct. I had deleted os.chdir(location) by mistake , hence was getting the error.
0

try the following:

from subprocess import check_output 
print(check_output(["dir", "C:\\Downloads\\file.csv"],shell=True).decode("utf8"))

ensure that you have \ in your path rather than '/' and "dir" rather "ls"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.