timmy 修订了这个 Gist 9 months ago. 转到此修订
1 file changed, 15 insertions
subprocess_example.py(文件已创建)
| @@ -0,0 +1,15 @@ | |||
| 1 | + | import subprocess | |
| 2 | + | ||
| 3 | + | # 執行系統指令並獲取輸出 | |
| 4 | + | result = subprocess.run(["ls", "-l"], capture_output=True, text=True) | |
| 5 | + | ||
| 6 | + | # 顯示執行結果 | |
| 7 | + | print("命令輸出:") | |
| 8 | + | print(result.stdout) | |
| 9 | + | ||
| 10 | + | # 檢查是否執行成功 | |
| 11 | + | if result.returncode == 0: | |
| 12 | + | print("命令執行成功") | |
| 13 | + | else: | |
| 14 | + | print(f"命令執行失敗,錯誤代碼: {result.returncode}") | |
| 15 | + | print("錯誤訊息:", result.stderr) | |
上一页
下一页