您现在的位置是:网站首页> 编程资料编程资料
Python subprocess库六个实例快速掌握_python_
2023-05-25
406人已围观
简介 Python subprocess库六个实例快速掌握_python_
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 NoneProcess 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
到此这篇关于Python subprocess库六个实例快速掌握的文章就介绍到这了,更多相关Python subprocess库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- Python async+request与async+aiohttp实现异步网络请求探索_python_
- python selenium参数详解和实现案例_python_
- Python OpenCV实现基于模板的图像拼接_python_
- python3使用mutagen进行音频元数据处理的方法_python_
- 基于Python实现自动关机小工具_python_
- AMP Tensor Cores节省内存PyTorch模型详解_python_
- Windows和夜神模拟器上抓包程序mitmproxy的安装使用详解_python_
- 关于pyinstaller 打包多个py文件的问题_python_
- 基于opencv对高空拍摄视频消抖处理方法_python_
- 详解Python如何轻松实现定时执行任务_python_
