網站首頁 編程語言 正文
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
原文鏈接:https://blog.csdn.net/Light2077/article/details/110913337
相關推薦
- 2022-12-12 Android?Google?AutoService框架使用詳解_Android
- 2022-10-15 詳解C++中常用的四種類型轉換方式_C 語言
- 2023-04-02 Android自定義View實現動畫效果詳解_Android
- 2022-05-13 (Qt)使用QCommandLineParser進行程序的命令行解析
- 2022-12-02 Android?使用?okhttp3和retrofit2?進行單文件和多文件上傳_Android
- 2022-10-28 Templates實戰之更優雅實現自定義View構造方法詳解_Android
- 2022-03-23 詳解C#異步多線程使用中的常見問題_C#教程
- 2022-10-03 正則表達式中^和$的含義與實例代碼_正則表達式
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支