检索 subprocess.call() 的输出[重复]

问题描述 投票:0回答:7

如何使用

subprocess.call()
获取进程运行的输出?

StringIO.StringIO
对象传递给
stdout
会出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>> 
python pipe subprocess stringio
7个回答
339
投票

如果你的Python版本> = 2.7,你可以使用

subprocess.check_output
,它基本上完全符合你的要求(它以字符串形式返回标准输出)。

简单示例(linux版本,见注释):

import subprocess

print subprocess.check_output(["ping", "-c", "1", "8.8.8.8"])

请注意,ping 命令使用 Linux 表示法(

-c
表示计数)。如果您在 Windows 上尝试此操作,请记住将其更改为
-n
以获得相同的结果。

正如下面评论的,您可以在这个其他答案中找到更详细的解释。


234
投票

来自

subprocess.call()
的输出只能重定向到文件。

您应该使用

subprocess.Popen()
来代替。然后,您可以传递
subprocess.PIPE
作为 stderr、stdout 和/或 stdin 参数,并使用
communicate()
方法从管道中读取:

from subprocess import Popen, PIPE

p = Popen(['program', 'arg1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode

推理是

subprocess.call()
使用的类文件对象必须有一个真实的文件描述符,从而实现
fileno()
方法。仅使用任何类似文件的对象都无法解决问题。

请参阅此处了解更多信息。


105
投票

对于Python 3.5或更高版本,建议您使用子进程模块中的run函数。这将返回一个

CompletedProcess
对象,您可以从中轻松获取输出以及返回代码。

from subprocess import PIPE, run

command = ['echo', 'hello']
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(result.returncode, result.stdout, result.stderr)

54
投票

我有以下解决方案。它捕获执行的外部命令的退出代码、标准输出和标准错误:

import shlex
from subprocess import Popen, PIPE

def get_exitcode_stdout_stderr(cmd):
    """
    Execute the external command and get its exitcode, stdout and stderr.
    """
    args = shlex.split(cmd)

    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    exitcode = proc.returncode
    #
    return exitcode, out, err

cmd = "..."  # arbitrary external command, e.g. "python mytest.py"
exitcode, out, err = get_exitcode_stdout_stderr(cmd)

我还有一篇关于它的博客文章这里

编辑:解决方案已更新为不需要写入临时文件的较新解决方案。文件。


31
投票

我最近刚刚弄清楚如何做到这一点,这是我当前项目中的一些示例代码:

#Getting the random picture.
#First find all pictures:
import shlex, subprocess
cmd = 'find ../Pictures/ -regex ".*\(JPG\|NEF\|jpg\)" '
#cmd = raw_input("shell:")
args = shlex.split(cmd)
output,error = subprocess.Popen(args,stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()
#Another way to get output
#output = subprocess.Popen(args,stdout = subprocess.PIPE).stdout
ber = raw_input("search complete, display results?")
print output
#... and on to the selection process ...

您现在已将命令的输出存储在变量“output”中。 “stdout = subprocess.PIPE”告诉类从 Popen 中创建一个名为“stdout”的文件对象。据我所知,communicate() 方法只是作为一种便捷的方式来返回输出的元组以及您运行的进程中的错误。此外,该进程在实例化 Popen 时运行。


29
投票

关键是使用功能

subprocess.check_output

例如,以下函数捕获进程的 stdout 和 stderr 并返回该结果以及调用是否成功。它兼容 Python 2 和 3:

from subprocess import check_output, CalledProcessError, STDOUT

def system_call(command):
    """ 
    params:
        command: list of strings, ex. `["ls", "-l"]`
    returns: output, success
    """
    try:
        output = check_output(command, stderr=STDOUT).decode()
        success = True 
    except CalledProcessError as e:
        output = e.output.decode()
        success = False
    return output, success

output, success = system_call(["ls", "-l"])

如果您想将命令作为字符串而不是数组传递,请使用此版本:

from subprocess import check_output, CalledProcessError, STDOUT
import shlex

def system_call(command):
    """ 
    params:
        command: string, ex. `"ls -l"`
    returns: output, success
    """
    command = shlex.split(command)
    try:
        output = check_output(command, stderr=STDOUT).decode()
        success = True 
    except CalledProcessError as e:
        output = e.output.decode()
        success = False
    return output, success

output, success = system_call("ls -l")

16
投票

Ipython
外壳中:

In [8]: import subprocess
In [9]: s=subprocess.check_output(["echo", "Hello World!"])
In [10]: s
Out[10]: 'Hello World!\n'

基于萨格的回答。感谢萨格。

© www.soinside.com 2019 - 2024. All rights reserved.