subprocess用法笔记

subprocess用法笔记classTest def init self path self path pathdeftest self p subprocess check output type self path shell True bytes 类型 输出全部 print p decod

class Test: def __init__(self, path): self.path = path def test(self): p = subprocess.check_output('type ' + self.path, shell=True) # bytes类型,输出全部 print(p.decode()) # bytes -> str def test1(self): p = subprocess.Popen('type ' + self.path, stdout=subprocess.PIPE, shell=True) a = p.stdout.read() # bytes类型,输出全部 print(a.decode()) # bytes -> str def test2(self): p = subprocess.Popen('type ' + self.path, stdout=subprocess.PIPE, shell=True) a = p.stdout.readline() # bytes类型,输出一行 print(a.decode()) # bytes -> str def test3(self): p = subprocess.Popen('type ' + self.path, stdout=subprocess.PIPE, shell=True) a = p.stdout.readlines() # list类型,输出全部 print(a) def test4(self): p = subprocess.Popen('type ' + self.path, stdout=subprocess.PIPE, shell=True) print(p.stdout) for i in p.stdout: print(i) # bytes类型,输出一行,同p.stdout.readline() 

1、连续执行shell命令

方法一:

cmds = [ b"alias ls='ls --color=never'", b"cd usr", b"ls", b"exit" # 这是是非常关键的,退出 ] pipe = subprocess.Popen("adb shell", stdin=subprocess.PIPE, stdout=subprocess.PIPE) out, err = pipe.communicate(b"\n".join(cmds) + b"\n") print(out) 

方法二:

p1 = subprocess.Popen("adb shell cd usr&&ls --color=never", stdout=subprocess.PIPE, shell=True) print(p1.stdout.read()) 

Linux/Mac 下还可以使用 ; 来链接两条命令,顺序执行命令,不管成功与否都往后执行,和 & 含义一样。


2、执行命令并持续获取返回值

import subprocess order = 'adb logcat' pi = subprocess.Popen(order, shell=True, stdout=subprocess.PIPE) for i in iter(pi.stdout.readline, 'b'): print(i) 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211451.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:24
下一篇 2026年3月18日 下午10:24


相关推荐

  • 《项目经理演讲与呈现技巧》总结

    《项目经理演讲与呈现技巧》总结

    2021年9月13日
    58
  • 怎么去掉织梦网站首页带的index.html/index.php

    怎么去掉织梦网站首页带的index.html/index.php

    2021年9月20日
    55
  • Gson json转list

    Gson json转list在日常应用中 我们一般都会碰到两种情况 转成单一实体对象和转换成对象列表或者其他结构 先来看第一种 比如 json 字符串为 name name0 age 0 Personperson gson fromJson str Person class 提供两个参数 分别是 json 字符串以及需要转换对象的类型 第二种 转换成列表类型 Lis

    2026年3月16日
    2
  • pycharm如何设置官方中文?pycharm如何汉化?pycharm终于支持官中了!!![通俗易懂]

    pycharm如何设置官方中文?pycharm如何汉化?pycharm终于支持官中了!!![通俗易懂]文章目录汉化方法官方汉化与第三方对比Java的idea在更新2020.1时就更新了官方汉化,当时Pycharm还没用出现汉化,但这两天提示我更新2020.1.1的时候,我发现pycharm也出现了官方汉化,在此建议你要是想用官方汉化,先把你的pycharm升级为最新版本。汉化方法打开pycharm左上角的file(文件)>选择settings(设置)>打开plugins(插件)>搜索chinese(中文插件),下载好后重启pycharm。官方汉化与第三方对比这里使用

    2022年5月26日
    35
  • html css制作404页面,CSS3绘制404页面

    html css制作404页面,CSS3绘制404页面标题有点噱了…最近在做一个交通有关的项目,想做一个类似标志牌的404,所以就有了这个.只用的CSS3中的旋转,效果如下上代码:Error.circle{width:200px;height:200px;border-radius:200px;border:15pxsolid#B22727;}.circle>div{color:#B22727;font:bol…

    2022年7月27日
    9
  • 使用 UpdatePanel

    使用 UpdatePanel1概述ASP.NETUpdatePanel控件能让你创建丰富的、以客户为中心的Web应用程序。使用UpdatePanel控件,可以刷新选择的页面部分而不是使用回发来刷新整个页面,这就像是执行了一个局部页面更新一样。包含一个ScriptManager和一个或多个UpdatePanel的Web页面会自动加入局部页面更新,而不需要定制客户端代码。1.1场景…

    2022年7月23日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号