Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 51ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
c

command-execution

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 subprocess 執行系統指令

Last active 5 hours ago automation command-execution python shell subprocess

subprocess 模組可用於執行系統指令、與外部程式互動,適用於自動化腳本、批次處理與系統管理等應用。

0 0 0 1
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 # 檢查是否執行成功