欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python subprocess庫六個實例快速掌握

 更新時間:2022年10月25日 14:19:24   作者:Light2077  
這次來說Python的第三方庫subprocess庫,在python2.4以上的版本commands模塊被subprocess取代了。一般當我們在用Python寫運維腳本時,需要履行一些Linux shell的命令,Python中subprocess模塊就是專門用于調(diào)用Linux shell命令,并返回狀態(tài)和結(jié)果,可以完美的解決這個問題

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'])

執(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)

運行后

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

returncode 表示你run的這個py文件過程是否正確,如果正確,返回0,否則返回1

例3-全面的返回值介紹

  • args:被用作啟動進程的參數(shù),可能是列表或字符串
  • returncode:子進程的退出狀態(tài)碼
  • stdout:從子進程捕獲到的標準輸出,但是沒設置subprocess.run()中的stdout參數(shù)時,這一項是None。
  • stderr:捕獲到的子進程標準錯誤,沒設置subprocess.run()中的stderr參數(shù)時,這一項是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 None

Process finished with exit code 0

可以看到,沒有設置subprocess.run()中的參數(shù)stdoutstderr時,這兩項都是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函數(shù),得到返回

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

可以看到,先是正確打印了hello.py的內(nèi)容,然后是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)

結(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'

可以通過res.stdoutres2.stderr分別拿到正確print的信息和錯誤信息。

同時可以發(fā)現(xiàn),子進程print和報錯內(nèi)容就不會在父進程打印輸出了。

注意這里的res.stdout是一串二進制字符串。如果設置encoding參數(shù),拿到的就是字符串。

res = subprocess.run(['python', 'hello.py'], 
                     stdout=subprocess.PIPE,
                     encoding='utf8')

例6-與子進程進行通信

可以通過subprocess.run()input參數(shù)給子進程發(fā)送消息。如果不設置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)

結(jié)果

hello input

Process finished with exit code 0

到此這篇關(guān)于Python subprocess庫六個實例快速掌握的文章就介紹到這了,更多相關(guān)Python subprocess庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用Tensorflow的隊列多線程讀取數(shù)據(jù)方式

    利用Tensorflow的隊列多線程讀取數(shù)據(jù)方式

    今天小編就為大家分享一篇利用Tensorflow的隊列多線程讀取數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python里dict變成list實例方法

    python里dict變成list實例方法

    在本篇內(nèi)容里小編給大家分析了關(guān)于python里dict變成list實例方法的實例內(nèi)容,對此有需要的朋友們可以參考學習下。
    2019-06-06
  • RSA加密算法Python實現(xiàn)方式

    RSA加密算法Python實現(xiàn)方式

    這篇文章主要介紹了RSA加密算法Python實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python實現(xiàn)推送百度鏈接的示例代碼

    Python實現(xiàn)推送百度鏈接的示例代碼

    有時為了提高搜索效率,也讓搜索引擎更容易發(fā)現(xiàn)自己的文章,我們需要將文章鏈接推送到百度站長平臺,起到快速收錄的目的。本文將主要介紹如何通過Python實現(xiàn)這一功能,需要的可以參考一下
    2021-12-12
  • python畫圖時設置分辨率和畫布大小的實現(xiàn)(plt.figure())

    python畫圖時設置分辨率和畫布大小的實現(xiàn)(plt.figure())

    這篇文章主要介紹了python畫圖時設置分辨率和畫布大小的實現(xiàn)(plt.figure()),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Python實現(xiàn)學校管理系統(tǒng)

    Python實現(xiàn)學校管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)學校管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • python超時重新請求解決方案

    python超時重新請求解決方案

    這篇文章主要介紹了python超時重新請求解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程

    在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程

    這篇文章主要介紹了在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程,Debian系統(tǒng)和Nginx服務器皆是高性能的選擇,需要的朋友可以參考下
    2015-04-04
  • VScode查看python f.write()的文件亂碼問題及解決方法

    VScode查看python f.write()的文件亂碼問題及解決方法

    這篇文章主要介紹了VScode查看python f.write()的文件亂碼問題及解決方法,本文通過圖文并茂的形式給大家分享解決方法,需要的朋友可以參考下
    2023-02-02
  • python pywinauto使用過程及問題小結(jié)

    python pywinauto使用過程及問題小結(jié)

    在pywinauto庫中,uia即UIAutomation,是微軟提供的用于用戶界面自動化測試和輔助功能訪問的技術(shù)框架,UIAutomation支持自動化腳本與各種UI元素交互,本文給大家介紹python pywinauto使用過程及問題小結(jié),感興趣的朋友一起看看吧
    2024-10-10

最新評論