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 .

4

3 回答 3

7

可能只是想使用check_output,但是,正如您所知,有一个方法subprocess.check_output,但直到 Python 2.7 ( http://docs.python.org/3/library/subprocess.html#subprocess.check_output )才定义

您甚至可能想要这个,它定义了模块中不存在的函数(即在 v2.7 之前运行)。

try: subprocess.check_output
except: subprocess.check_output = check_output
subprocess.check_output()
于 2013-07-09T04:39:30.870 回答
6

只需使用:

check_output(['./MyFile'])

您已经定义了自己的函数,它不是subprocess模块的属性(对于 Python 2.6 及更早版本)。

您还可以将函数分配给导入的模块对象(但这不是必需的):

subprocess.check_output = check_output
location = "%s/folder" % (os.environ["Home"])
subprocess.check_output(['./MyFile'])
于 2013-07-09T04:37:02.443 回答
1

尝试以下操作:

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

确保您\在路径中而不是“/”和“dir”而不是“ls”

于 2019-12-10T05:56:22.647 回答