網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
subprocess
官方中文文檔
介紹參考文檔,我的直觀感受和實(shí)際用法是:subprocess可以開(kāi)啟一個(gè)子進(jìn)程來(lái)運(yùn)行cmd命令。那就意味著可以在一個(gè)py文件里運(yùn)行另一個(gè)py文件
例1-快速使用subprocess
新建一個(gè)目錄,目錄下有兩個(gè)文件
|-demo
? ? |-main.py
? ? |-hello.py
在hello.py
中
# hello.py print('hello world!')
在main.py
中
import subprocess subprocess.run(['python', 'hello.py'])
執(zhí)行main.py
文件得到如下結(jié)果
hello world!
例2-subprocess.run()的返回值
修改代碼如下:
# main.py import subprocess res = subprocess.run(['python', 'hello.py']) print("args:", res.args) print("returncode", res.returncode)
運(yùn)行后
hello world!
args: ['python', 'hello.py']
returncode: 0
returncode 表示你run的這個(gè)py文件過(guò)程是否正確,如果正確,返回0,否則返回1
例3-全面的返回值介紹
-
args
:被用作啟動(dòng)進(jìn)程的參數(shù),可能是列表或字符串 -
returncode
:子進(jìn)程的退出狀態(tài)碼 -
stdout
:從子進(jìn)程捕獲到的標(biāo)準(zhǔn)輸出,但是沒(méi)設(shè)置subprocess.run()
中的stdout
參數(shù)時(shí),這一項(xiàng)是None
。 -
stderr
:捕獲到的子進(jìn)程標(biāo)準(zhǔn)錯(cuò)誤,沒(méi)設(shè)置subprocess.run()
中的stderr
參數(shù)時(shí),這一項(xiàng)是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)
結(jié)果:
hello world!
args: ['python', 'hello.py']
returncode 0
stdout None
stderr NoneProcess finished with exit code 0
可以看到,沒(méi)有設(shè)置subprocess.run()
中的參數(shù)stdout
和stderr
時(shí),這兩項(xiàng)都是None
例4-代碼有bug的情況
新建fail.py
,故意制造一個(gè)bug
# fail.py a =
修改main.py
# main.py import subprocess res = subprocess.run(['python', 'hello.py']) res2 = subprocess.run(['python', 'fail.py'])
再運(yùn)行main函數(shù),得到返回
hello world!
? File "fail.py", line 2
? ? a =
? ? ? ^
SyntaxError: invalid syntax
可以看到,先是正確打印了hello.py
的內(nèi)容,然后是fail.py
的錯(cuò)誤信息。
例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)
結(jié)果
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'
可以通過(guò)res.stdout
與res2.stderr
分別拿到正確print的信息和錯(cuò)誤信息。
同時(shí)可以發(fā)現(xiàn),子進(jìn)程print和報(bào)錯(cuò)內(nèi)容就不會(huì)在父進(jìn)程打印輸出了。
注意這里的res.stdout
是一串二進(jìn)制字符串。如果設(shè)置encoding
參數(shù),拿到的就是字符串。
res = subprocess.run(['python', 'hello.py'], stdout=subprocess.PIPE, encoding='utf8')
例6-與子進(jìn)程進(jìn)行通信
可以通過(guò)subprocess.run()
的input
參數(shù)給子進(jìn)程發(fā)送消息。如果不設(shè)置encoding,就要傳入二進(jìn)制串,比如b'hello input'
# main.py import subprocess from subprocess import PIPE res = subprocess.run(['python', 'hello.py'], input='hello input', encoding='utf8')
修改hello.py
接收傳進(jìn)來(lái)的字符串。
# hello.py import sys data = sys.stdin.read() print(data)
結(jié)果
hello input
Process finished with exit code 0
原文鏈接:https://blog.csdn.net/Light2077/article/details/110913337
相關(guān)推薦
- 2023-10-16 springboot 集成webservice
- 2022-08-07 pd.DataFrame中的幾種索引變換的實(shí)現(xiàn)_python
- 2022-07-26 正則表達(dá)式規(guī)則
- 2022-07-07 圖解AVL樹(shù)數(shù)據(jù)結(jié)構(gòu)輸入與輸出及實(shí)現(xiàn)示例_C 語(yǔ)言
- 2022-04-15 Android開(kāi)發(fā)Jetpack組件WorkManager用例詳解_Android
- 2023-02-10 python使用xlsx和pandas處理Excel表格的操作步驟_python
- 2022-06-13 ASP.NET?Core中的Caching組件簡(jiǎn)介_(kāi)實(shí)用技巧
- 2023-01-19 Android?任務(wù)棧機(jī)制詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支