python?subprocess.run中的具體使用
一、詳解
subprocess.run 是 Python 3.5 及以上版本中引入的一個函數(shù),用于運行子進程。它是 subprocess 模塊的一部分,提供了一種更簡單和更強大的方式來創(chuàng)建和管理子進程。subprocess.run 函數(shù)可以替代舊的 os.system 和 subprocess.call 等函數(shù)。
以下是 subprocess.run 函數(shù)的詳細解釋:
1.1、基本用法
import subprocess result = subprocess.run(['ls', '-l'])
在這個例子中,subprocess.run 執(zhí)行了 ls -l 命令,并等待命令完成。
1.2、參數(shù)詳解
- args: 要執(zhí)行的命令及其參數(shù)??梢允且粋€字符串或一個字符串列表。
- stdin, stdout, stderr: 分別表示標準輸入、標準輸出和標準錯誤??梢允?subprocess.PIPE、文件對象或文件描述符。
- input: 傳遞給子進程的輸入數(shù)據(jù)。
- timeout: 子進程的超時時間(以秒為單位)。如果子進程在指定時間內沒有完成,將拋出 subprocess.TimeoutExpired 異常。
- check: 如果設置為 True,并且子進程返回一個非零退出狀態(tài)碼,將拋出 subprocess.CalledProcessError 異常。
- encoding: 如果指定了 encoding,則 stdin, stdout, stderr 將被解碼為字符串。
- errors: 用于指定如何處理解碼錯誤。
- shell: 如果設置為 True,將通過 shell 執(zhí)行命令。
- cwd: 設置子進程的當前工作目錄。
- env: 用于指定子進程的環(huán)境變量。
1.3、返回值
subprocess.run 返回一個 CompletedProcess 對象,包含以下屬性:
- args: 執(zhí)行的命令及其參數(shù)。
- returncode: 子進程的退出狀態(tài)碼。
- stdout: 子進程的標準輸出(如果 stdout 參數(shù)設置為 subprocess.PIPE)。
- stderr: 子進程的標準錯誤(如果 stderr 參數(shù)設置為 subprocess.PIPE)。
1.4、示例
捕獲輸出
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) print(result.stdout)
處理錯誤
try: result = subprocess.run(['ls', '-l', '/nonexistent'], check=True) except subprocess.CalledProcessError as e: print(f"Error: {e}")
使用超時
try: result = subprocess.run(['sleep', '10'], timeout=5) except subprocess.TimeoutExpired as e: print(f"Timeout: {e}")
通過 shell 執(zhí)行命令
result = subprocess.run('echo $HOME', shell=True, text=True) print(result.stdout)
1.5、總結
subprocess.run 提供了一種靈活且強大的方式來執(zhí)行子進程,并且可以通過各種參數(shù)來控制子進程的行為和處理其輸出。通過合理使用這些參數(shù),可以滿足大多數(shù)子進程管理的需求。
二、subprocess.run執(zhí)行python文件
在 Python 中,你可以使用 subprocess.run 來執(zhí)行另一個 Python 文件。subprocess.run 是 Python 3.5 及以上版本中推薦的用于運行子進程的函數(shù)。下面是一個示例,展示了如何使用 subprocess.run 來執(zhí)行另一個 Python 文件:
假設你有一個名為 script_to_run.py 的 Python 文件,你想從另一個 Python 腳本中運行它。
1、創(chuàng)建一個名為 script_to_run.py 的文件,并在其中編寫一些代碼,例如:
# script_to_run.py print("Hello from script_to_run.py!")
2、創(chuàng)建另一個 Python 文件,例如 main_script.py,并在其中使用 subprocess.run 來執(zhí)行 script_to_run.py:
# main_script.py import subprocess # 使用 Python 解釋器執(zhí)行 script_to_run.py result = subprocess.run(['python', 'script_to_run.py'], capture_output=True, text=True) # 打印子進程的輸出 print("Output of script_to_run.py:") print(result.stdout) # 打印子進程的錯誤輸出(如果有) if result.stderr: print("Errors:") print(result.stderr)
在這個示例中,subprocess.run 函數(shù)的參數(shù)解釋如下:
- ['python', 'script_to_run.py']:這是一個列表,包含要執(zhí)行的命令和參數(shù)。在這里,我們使用 python 解釋器來運行 script_to_run.py 文件。
- capture_output=True:這個參數(shù)告訴 subprocess.run 捕獲子進程的標準輸出和標準錯誤。
- text=True:這個參數(shù)告訴 subprocess.run 將輸出和錯誤作為字符串而不是字節(jié)返回。
運行 main_script.py,你應該會看到以下輸出:
Output of script_to_run.py: Hello from script_to_run.py!
如果 script_to_run.py 中有任何錯誤輸出,它們也會被捕獲并打印出來。
請注意,如果你使用的是 Python 3.6 或更高版本,你可以使用 subprocess.run 的 check=True 參數(shù)來自動檢查子進程的返回碼,并在子進程返回非零狀態(tài)碼時引發(fā) subprocess.CalledProcessError 異常:
# main_script.py import subprocess try: result = subprocess.run(['python', 'script_to_run.py'], capture_output=True, text=True, check=True) print("Output of script_to_run.py:") print(result.stdout) except subprocess.CalledProcessError as e: print("Errors:") print(e.stderr)
這樣可以更方便地處理子進程執(zhí)行失敗的情況。
到此這篇關于python subprocess.run中的具體使用的文章就介紹到這了,更多相關python subprocess.run內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python django事務transaction源碼分析詳解
這篇文章主要介紹了python django事務transaction源碼分析詳解的相關資料,需要的朋友可以參考下2017-03-03使用Python實現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫,文中的示例代碼講解詳細,有需要的小伙伴可以參考一下2024-04-04詳解Python開發(fā)語言中的基本數(shù)據(jù)類型
數(shù)據(jù)類型想必大家都知道是什么含義,指的是輸入數(shù)據(jù)的類型,任何數(shù)據(jù)都有明確的數(shù)據(jù)類型。本文主要和大家聊聊Python的三種基本數(shù)據(jù)類型,感興趣的可以了解一下2022-10-10