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

Vik*_*lyi 22 python subprocess adb

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

import subprocess

...

def exec_adb_command(adb_command):
    return = subprocess.call(adb_command)
Run Code Online (Sandbox Code Playgroud)

如果命令执行propery exec_adb_command返回0即可.

但是一些adb命令不仅会返回"0"或"1",还会生成一些我想要捕获的输出.adb设备例如:

D:\git\adb-lib\test>adb devices
List of devices attached
07eeb4bb        device
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢!

Pad*_*ham 33

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

您还需要重定向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()
Run Code Online (Sandbox Code Playgroud)

只需传递你的命令:

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')
Run Code Online (Sandbox Code Playgroud)