Python,ADB和Shell执行查询

编程入门 行业动态 更新时间:2024-10-27 02:18:18
本文介绍了Python,ADB和Shell执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对Python不太熟悉,但对Ruby却很熟悉。因此,我将提供我想要实现的目标的类似物

I don't have a lot of familiarity with Python but I do with Ruby. So I will provide analogues for what I want to achieve

在Ruby中,我会

val = `adb devices`

获取存储的adb设备的原始输出在val中,并且

to get the "raw output" of adb devices stored in val, and

val=system("adb devices")

获取状态代码

我想在Python中执行相同的任务。我看着

I want to perform the same tasks in Python. I looked at

from subprocess import call call(["adb devices"])

但是失败了,我不想使用os.system,因为我想得到一些适当的错误处理。我如何进行呼叫工作以及如何从Python中的反引号获取原始输出

but that failed, I don't want to use os.system cause I want to get some proper error handling. how do i make call work and how do i obtain raw output from backticks in Python

推荐答案

将命令和参数作为单独的元素传递列表中的内容:

Pass the command and arguments as separate elements of a list:

from subprocess import call return_code = call(["adb", "devices"])

但是,这会将输出发送到stdout,您无法捕获它。相反,您可以使用 subprocess.check_ouput() :

However, this sends the output to stdout and you can't capture it. Instead you can use subprocess.check_ouput():

from subprocess import check_output adb_ouput = check_output(["adb", "devices"]) # stdout of adb in adb_output.

如果返回码非零,则会引发异常。您应该抓住它以查看返回码是什么。返回代码0不会引发异常:

If the return code is non-zero an exception is raised. You should catch it to see what the return code is. No exception is raised for return code 0:

from subprocess import check_output, CalledProcessError try: adb_ouput = check_output(["adb", "devices"]) except CalledProcessError as e: print e.returncode

更多推荐

Python,ADB和Shell执行查询

本文发布于:2023-10-07 14:46:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   ADB   Shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!