subprocess用法笔记

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)

参考
https://blog.csdn.net/telenewbie/article/details/60580727

方法二:

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

同时执行多行命令:
无论是 Linux/Mac 还是 Windows 的 shell 命令都支持一条命令来执行多条命令的。一共有 &&,&,||,| 这么几种方式,这几种方式分别代表着不同的含义:

&&:command1 && command2,如果 command1 执行成功了,就执行命令 command2,如果 command1 失败了,就不会执行 command2 了。
&:command1 & command2,无论 command1 执行成功与否都会执行 command2。
||:command1 || command2,如果 command1 执行成功了,就不会执行 command2 了,如果 command1 失败了,就会继续执行 command2。
|:command1 | command2,command1 的结果做为 command2 的参数,如果 command1 失败了,整个命令也就都失败了。

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

参考
http://blog.sina.com.cn/s/blog_44d19b500102w585.html


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)

参考
http://blog.sina.com.cn/s/blog_44d19b500102x21i.html

subprocess是Python中用于执行外部命令的模块。根据引用中的示例,可以看到subprocess.run()函数可以执行各种命令,并返回执行结果。例如,可以使用subprocess.run(["ls", "-l"])来执行"ls -l"命令,并将结果打印出来。同样,使用subprocess.run("exit 1", shell=True, check=True)可以捕获命令执行过程中的错误。还可以使用subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)来执行命令并捕获输出。 另外,引用中的示例演示了如何获取当前执行子shell的程序的进程号。可以使用subprocess.Popen()函数来创建一个子进程,并使用res.pid来获取进程号。 引用中的示例展示了如何结束一个正在运行的进程。可以使用res.terminate()来终止一个进程的执行。 综上所述,subprocess模块提供了丰富的功能,可以用于执行外部命令,并控制和管理进程的执行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python——系统交互subprocess](https://blog.csdn.net/zangba9624/article/details/109529721)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [subprocess模块功能与常见用法实例详解](https://blog.csdn.net/yunweimao/article/details/106687894)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值