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

本文探讨了在Python中使用subprocess模块时遇到的问题,特别是当尝试在Python 2.6.7环境中运行hadoop命令时出现的AttributeError。文章提供了详细的解决方案,包括如何使用Popen和communicate方法来代替check_output。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. why?



















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?

share improve this question what version of python on the remote machine?  –  Padraic Cunningham   Nov 12 '14 at 18:37
  • @PadraicCunningham 2.6.7 –  Liondancer  Nov 12 '14 at 18:37

2 Answers

up vote 16 down vote accepted

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值