subprocess.check_output()模块对象具有out属性'check_output' [英] subprocess.check_output() module object has out attribute 'check_output'

查看:313
本文介绍了subprocess.check_output()模块对象具有out属性'check_output'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩子进程库。我可以执行

v = subprocess.check_output(ls)



,它工作正常。然而,当我SSH进入一台机器,并且我想执行一个hadoop命令时,

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



我得到

  Traceback(最近一次调用的最后一个):
在< module>中的第1行文件< stdin>
AttributeError:'module'object has no attribute'check_output'

不太清楚原因。我想这可能是我对子过程缺乏了解。如何解决这个错误?

解决方案

check_output code> python 2.7 所以对于不适用>< 2.7



您可以使用 Popen / code>来获得输出。

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

部分docs可能有用

I am playing with the subprocess library. I can perform

v = subprocess.check_output("ls")

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

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?

解决方案

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

You can use Popen with communicate to get the output.

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

This part of the docs may be useful

这篇关于subprocess.check_output()模块对象具有out属性'check_output'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆