我一直在阅读有关子进程模块的Python文档(请参阅here),它会讨论subprocess.check_output()
命令,这似乎正是我需要的。
但是,当我尝试使用它时,我收到一个不存在的错误,当我运行dir(subprocess)
时,它没有列出。
我正在运行Python 2.6.5,我使用的代码如下:
import subprocess
subprocess.check_output(["ls", "-l", "/dev/null"])
有谁知道为什么会这样?
答案 0 :(得分:117)
它是在2.7中引入的。见docs。
如果您想要输出,请使用subprocess.Popen:
>>> import subprocess
>>> output = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE).communicate()[0]
答案 1 :(得分:55)
如果它在您要运行的代码中大量使用,但该代码不必长期维护(或者您需要快速修复而不管潜力如何未来的维护难题)那么你可以在导入子进程的任何地方使用它(也就是猴子补丁)......
只需将代码从2.7抬起并插入即可......
import subprocess
if "check_output" not in dir( subprocess ): # duck punch it in!
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
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]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
可能需要轻微的烦躁不安。
请记住,尽管你有责任保持像这样的肮脏的小后端。如果在最新的python中发现并纠正了错误,那么你a)必须注意到这一点,并且b)如果你想保持安全,请更新你的版本。另外,覆盖&自己定义内部功能是下一个人最糟糕的噩梦,特别是下一个人是你几年后,你已经忘记了上次你做的那些肮脏的黑客!总之:它很少是一个好主意。
答案 2 :(得分:6)
感谢猴子补丁建议(我的尝试失败了 - 但是我们消耗了CalledProcessError输出,因此需要修补它)
在这里找到了一个有效的2.6补丁: http://pydoc.net/Python/pep8radius/0.9.0/pep8radius.shell/
"""Note: We also monkey-patch subprocess for python 2.6 to
give feature parity with later versions.
"""
try:
from subprocess import STDOUT, check_output, CalledProcessError
except ImportError: # pragma: no cover
# python 2.6 doesn't include check_output
# monkey patch it in!
import subprocess
STDOUT = subprocess.STDOUT
def check_output(*popenargs, **kwargs):
if 'stdout' in kwargs: # pragma: no cover
raise ValueError('stdout argument not allowed, '
'it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE,
*popenargs, **kwargs)
output, _ = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd,
output=output)
return output
subprocess.check_output = check_output
# overwrite CalledProcessError due to `output`
# keyword not being available (in 2.6)
class CalledProcessError(Exception):
def __init__(self, returncode, cmd, output=None):
self.returncode = returncode
self.cmd = cmd
self.output = output
def __str__(self):
return "Command '%s' returned non-zero exit status %d" % (
self.cmd, self.returncode)
subprocess.CalledProcessError = CalledProcessError