subprocess.check_output()模块对象具有out属性'check_output' [英] subprocess.check_output() module object has out attribute '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屋!