Python subprocess.check_output() 模块对象有属性“check_output”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26894024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:07:43  来源:igfitidea点击:

subprocess.check_output() module object has out attribute 'check_output'

pythonhadoop

提问by Liondancer

I am playing with the subprocess library. I can perform

我正在玩子进程库。我可以表演

v = subprocess.check_output("ls")

v = subprocess.check_output("ls")

and it works fine. However, when I SSH into a machine and I want to perform a hadoop command,

它工作正常。但是,当我通过 SSH 连接到一台机器并且我想执行一个 hadoop 命令时,

v = subprocess.check_output("hadoop fs -ls /path/to/file* | grep -oE '/(.*)'")

v = subprocess.check_output("hadoop fs -ls /path/to/file* | grep -oE '/(.*)'")

I get

我得到

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'check_output'

Not quite sure why. I think it might be my lack of understanding of subprocess. How can i fix this error?

不太清楚为什么。我想这可能是我对子流程缺乏了解。我该如何解决这个错误?

采纳答案by Padraic Cunningham

check_outputwas introduced in python 2.7so won't work for < 2.7.

check_output被引入,python 2.7因此不适用于< 2.7.

You can use Popenwith communicateto get the output.

您可以使用Popenwithcommunicate来获取输出。

from subprocess import PIPE,Popen
proc = Popen(['ls', '-l'], stdout=PIPE)
print(proc.communicate()[0].split())

Thispart of the docs may be useful

部分文档可能有用

回答by v.babak

Here is my version of subprocess.check_outputfunction for Python 2.6 and it supports pipe lines along with habitual command interface.

这是我用于 Python 2.6的subprocess.check_output函数版本,它支持管道以及习惯的命令界面。

import subprocess

def check_output(cmd):
    """ https://docs.python.org/2/library/subprocess.html#subprocess.Popen
        Implementation subprocess.check_output() for Python 2.6
     """
    process_list = []
    cmd_list = cmd.strip().split("|")
    for i, sub_cmd in enumerate(cmd_list):
        STDIN = None
        if i > 0:
            STDIN = process_list[i - 1].stdout
        process_list.append(subprocess.Popen(sub_cmd, stdin=STDIN, stdout=subprocess.PIPE, shell=True))
    if len(process_list) == 0:
        return ''
    output = process_list[i].communicate()[0]
    return output

Test call:

测试调用:

print check_output('ls -la /var | grep log')

Output:

输出:

drwxr-xr-x 1 root root 4096 Dec 11 13:22 log

drwxr-xr-x 1 根 4096 12 月 11 日 13:22 日志

回答by MVanOrder

padraic-cunningham, while your answer got me part of the way there, in my case the command is an SSH command. Since communicate kills my process prematurely(as soon as it reads an EOF character), I've found the following works better:

padraic-cunningham,虽然你的回答让我在那里做了一部分,但在我的情况下,命令是一个 SSH 命令。由于通信过早地终止了我的进程(一旦它读取 EOF 字符),我发现以下方法效果更好:

import subprocess

def check_output(command_list):
    proc = subprocess.Popen(command_list, stdout=subprocess.PIPE)
    proc.wait()
    return proc.stdout.read()