6个例子快速学会python中subprocess库的使用

您所在的位置:网站首页 ps修图软件怎样修图片大小尺寸 6个例子快速学会python中subprocess库的使用

6个例子快速学会python中subprocess库的使用

2024-01-05 06:30| 来源: 网络整理| 查看: 265

subprocess

官方中文文档

介绍参考文档,我的直观感受和实际用法是:subprocess可以开启一个子进程来运行cmd命令。那就意味着可以在一个py文件里运行另一个py文件

例1:快速使用subprocess

新建一个目录,目录下有两个文件

|-demo |-main.py |-hello.py

在hello.py中

# hello.py print('hello world!')

在main.py中

import subprocess subprocess.run(['python', 'hello.py'])

执行main.py文件得到如下结果

hello world! 例2:subprocess.run()的返回值

修改代码如下:

# main.py import subprocess res = subprocess.run(['python', 'hello.py']) print("args:", res.args) print("returncode", res.returncode)

运行后

hello world! args: ['python', 'hello.py'] returncode: 0

returncode 表示你run的这个py文件过程是否正确,如果正确,返回0,否则返回1

例3:全面的返回值介绍 args:被用作启动进程的参数,可能是列表或字符串returncode:子进程的退出状态码stdout:从子进程捕获到的标准输出,但是没设置subprocess.run()中的stdout参数时,这一项是None。stderr:捕获到的子进程标准错误,没设置subprocess.run()中的stderr参数时,这一项是None。check_returncode():如果 returncode 非零, 抛出 CalledProcessError.

修改main.py

# main.py import subprocess res = subprocess.run(['python', 'hello.py']) print("args:", res.args) print("returncode", res.returncode) print("stdout", res.stdout) print("stderr", res.stderr)

结果:

hello world! args: ['python', 'hello.py'] returncode 0 stdout None stderr None Process finished with exit code 0

可以看到,没有设置subprocess.run()中的参数stdout和stderr时,这两项都是None

例4:代码有bug的情况

新建fail.py,故意制造一个bug

# fail.py a =

修改main.py

# main.py import subprocess res = subprocess.run(['python', 'hello.py']) res2 = subprocess.run(['python', 'fail.py'])

再运行main函数,得到返回

hello world! File "fail.py", line 2 a = ^ SyntaxError: invalid syntax

可以看到,先是正确打印了hello.py的内容,然后是fail.py的错误信息。

例5:捕获stdout和stderr

修改main.py

# main.py import subprocess res = subprocess.run(['python', 'hello.py'], stdout=subprocess.PIPE) res2 = subprocess.run(['python', 'fail.py'], stderr=subprocess.PIPE) print('hello.py stdout:', res.stdout) print('fail.py stderr:', res2.stderr)

结果

hello.py stdout: b'hello world!\r\n' fail.py stderr: b' File "fail.py", line 2\r\n a =\r\n ^\r\nSyntaxError: invalid syntax\r\n'

可以通过res.stdout与res2.stderr分别拿到正确print的信息和错误信息。

同时可以发现,子进程print和报错内容就不会在父进程打印输出了。

注意这里的res.stdout是一串二进制字符串。如果设置encoding参数,拿到的就是字符串。

res = subprocess.run(['python', 'hello.py'], stdout=subprocess.PIPE, encoding='utf8') 例6:与子进程进行通信

可以通过subprocess.run()的input参数给子进程发送消息。如果不设置encoding,就要传入二进制串,比如b'hello input'

# main.py import subprocess from subprocess import PIPE res = subprocess.run(['python', 'hello.py'], input='hello input', encoding='utf8')

修改hello.py接收传进来的字符串。

# hello.py import sys data = sys.stdin.read() print(data)

结果

hello input Process finished with exit code 0


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3