共計 488 個字符,預(yù)計需要花費 2 分鐘才能閱讀完成。
Python 中可以使用 subprocess
模塊來調(diào)用 shell 命令。以下是一個簡單的示例代碼:
import subprocess
# 執(zhí)行一個簡單的 shell 命令
subprocess.call('ls')
# 傳遞參數(shù)給 shell 命令
subprocess.call('echo Hello, World!', shell=True)
# 保存 shell 命令的輸出
output = subprocess.check_output('ls')
print(output.decode('utf-8'))
在上面的示例中,subprocess.call
函數(shù)可以執(zhí)行一個 shell 命令,并返回命令的退出狀態(tài)碼。subprocess.check_output
函數(shù)用于執(zhí)行一個 shell 命令并返回其輸出。需要注意的是,subprocess.call
和 subprocess.check_output
函數(shù)的參數(shù)應(yīng)該是字符串形式的 shell 命令,如果命令包含空格或特殊字符,可以設(shè)置 shell=True
參數(shù)。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完