如何从Python中获取子进程的返回代码和输出?

时间:2015-06-19 12:10:56

标签: python subprocess adb

在为Android Debug Bridge(ADB)开发python包装器库时,我使用 subprocess 在shell中执行adb命令。这是简化的例子:

import subprocess

...

def exec_adb_command(adb_command):
    return = subprocess.call(adb_command)

如果命令执行了 exec_adb_command ,则返回0,这是正常的。

但是有些adb命令不仅会返回" 0"或" 1"但也产生一些我想要捕获的输出。 adb devices 例如:

D:\git\adb-lib\test>adb devices
List of devices attached
07eeb4bb        device

我已经为此尝试了 subprocess.check_output(),它确实返回输出但不返回代码(" 0"或" 1"。)

理想情况下,我想得到一个元组,其中t [0]是返回码,t [1]是实际输出。

我是否遗漏了子流程模块中已经允许获得此类结果的内容?

谢谢!

1 个答案:

答案 0 :(得分:30)

Popen和communication将允许您获取输出和返回代码。

from subprocess import Popen,PIPE,STDOUT

out = Popen(["adb", "devices"],stderr=STDOUT,stdout=PIPE)

t = out.communicate()[0],out.returncode
print(t)
('List of devices attached \n\n', 0)

check_output也可能合适,非零退出状态会引发CalledProcessError:

from subprocess import check_output, CalledProcessError

try:
    out = check_output(["adb", "devices"])
    t = 0, out
except CalledProcessError as e:
    t = e.returncode, e.message

您还需要重定向stderr以存储错误输出:

from subprocess import check_output, CalledProcessError

from tempfile import TemporaryFile

def get_out(*args):
    with TemporaryFile() as t:
        try:
            out = check_output(args, stderr=t)
            return  0, out
        except CalledProcessError as e:
            t.seek(0)
            return e.returncode, t.read()

只需传递命令:

In [5]: get_out("adb","devices")
Out[5]: (0, 'List of devices attached \n\n')

In [6]: get_out("adb","devices","foo")
Out[6]: (1, 'Usage: adb devices [-l]\n')