Python subprocess 使用(一)

本文主要讲下 subprocess 的简单使用.

1: 通过subprocess 获取设备信息

import subprocess
def get_android_device_info():
# 使用adb命令获取设备信息
result = subprocess.run(['adb', 'devices', '-l'], capture_output=True, text=True)
output = result.stdout.strip() # 解析设备信息
devices = []
lines = output.split('\n')
for line in lines[1:]:
if 'device' in line:
device_info = line.split()
device = {
'serial': device_info[0],
'model': device_info[4],
'device': device_info[5],
'product': device_info[3],
'transport_id': device_info[6]
}
devices.append(device) return devices # 调用函数获取设备信息
devices = get_android_device_info()
for device in devices:
print('Serial:', device['serial'])
print('Model:', device['model'])
print('Device:', device['device'])
print('Product:', device['product'])
print('Transport ID:', device['transport_id'])
print('--')

具体的adb 执行结果如下:

List of devices attached
6b4a96b2 device usb:1-1 product:LeMax2_WW model:LEX820 device:le_x2 transport_id:43

2: 通过subprocess 获取屏幕分辨率

def get_device_physical():
command = 'adb shell wm size'
result = subprocess.check_output(command.split()).decode().strip()
resolution = result.split()[-1]
return resolution print('设备分辨率:' + str(get_device_physical()))

3: 获取设备系统日志

def capture_android_log():
# 使用adb命令获取Android系统日志
command = "adb logcat"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 读取日志输出
while True:
line = process.stdout.readline().decode("utf-8")
if line:
print(line.strip())
else:
break # 调用函数抓取Android系统日志
capture_android_log()

Python subprocess 使用(一)的更多相关文章

  1. 【python库模块】Python subprocess模块功能与常见用法实例详解

    前言 这篇文章主要介绍了Python subprocess模块功能与常见用法,结合实例形式详细分析了subprocess模块功能.常用函数相关使用技巧. 参考 1. Python subprocess ...

  2. python subprocess popen 静默模式(不弹出console控制台)

    python subprocess popen 静默模式(不弹出console控制台) import subprocess,sys IS_WIN32 = 'win32' in str(sys.plat ...

  3. python subprocess相关操作

    python subprocess常用操作 1.subprocess模块的常用函数 函数 描述 subprocess.run() Python 3.5中新增的函数.执行指定的命令,等待命令执行完成后返 ...

  4. Python subprocess.Popen communicate() 和wait()使用上的区别

    之所以会纠结到这个问题上是因为发现在调用Popen的wait方法之后程序一直没有返回.google发现wait是有可能产生死锁的.为了把这个问题彻底弄清楚,搜索一些资料过来看看: 原文链接:http: ...

  5. python subprocess阻塞

    import select import os import subprocess import time import fcntl args = ['python','./fetch_file2.p ...

  6. python subprocess 自动运行实验室程序

    import threading, os, subprocess, time exec_path = "/home/xhz/gems/ruby/amd...../bin/tester.exe ...

  7. Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

  8. python subprocess重定向标准输出

    subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = s ...

  9. Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects

    Posted: 2009-04-28 15:20 Tags: Python Note Much of the "What Happens When you Execute a Command ...

  10. 【转】 Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

随机推荐

  1. utils工具类整理

    闲暇之余,整理出了项目中常用的一些工具类,不是很全,后续会持续更新--- 全部代码请移植github哦-github地址:https://github.com/yang302/utils

  2. Htttpclien循环自动生成图片,同时发送参数和文件,模拟http的post请求

    package org.jeecg.modules.bussiness.PostTests; import com.sun.tools.internal.xjc.reader.xmlschema.bi ...

  3. 全局安装oh-my-zsh保姆教程

    我的系统是CentOS 7.6,按流程走完后可以实现系统内所有用户都默认使用zsh且插件配置共享省去重复编写配置或软连接的烦恼 1 安装git yum -y install git 2 安装zsh y ...

  4. Solution Set -「ABC 192」

    「ABC 113A」Star Link. 略. #include<cstdio> int x; int main() { scanf("%d",&x); for ...

  5. 《流畅的Python》 读书笔记 230926

    写在最前面的话 缘由 关于Python的资料市面上非常多,好的其实并不太多. 个人认为,基础的,下面的都还算可以 B站小甲鱼 黑马的视频 刘江的博客 廖雪峰的Python课程 进阶的更少,<流畅 ...

  6. 解决SpringBoot3.X中starter配置自动注入失效问题

    在自定义 starter 项目时,如果组件无法被 @ComponentScan 扫描并且想自动注册到 IOC 中,在springboot2.7之前 我们会采用 spring,factories 方式, ...

  7. 【Flutter】如何优美地实现一个悬浮NavigationBar

    [Flutter]如何优美地实现一个悬浮NavigationBar 最近写代码的时候遇到了一个如下的需求: 整体来说,底部的条是一个浮动的悬浮窗,有如下的三个按钮: 点击左边的要进入"主页& ...

  8. os --- 多种操作系统接口¶

    os.path --- 常用路径操作 源代码: Lib/posixpath.py (用于 POSIX) 和 Lib/ntpath.py (用于 Windows). 此模块实现了一些有用的路径名称相关函 ...

  9. salesforce零基础学习(一百三十三)ListView的button思考

    本篇参考: salesforce零基础学习(九十五)lightning out salesforce零基础学习(一百一十)list button实现的一些有趣事情 https://help.sales ...

  10. 小白CNN入门指导

    小白CNN入门指导 这几天一直在小白入门学习卷积层以准备组会,以下是我自学理解内容,若有错误的地方请各位评论指出 数学部分 一 卷积层 \[输入 32*32*3 (input neurons) \] ...