共計 349 個字符,預(yù)計需要花費 1 分鐘才能閱讀完成。
在 Python 中執(zhí)行 cmd 命令,可以使用 os
模塊中的 system()
或 popen()
方法。
下面是使用 os.system()
方法執(zhí)行 cmd 命令的示例:
import os
# 執(zhí)行簡單的 cmd 命令
os.system("dir")
# 執(zhí)行帶參數(shù)的 cmd 命令
os.system("ping 127.0.0.1")
下面是使用 os.popen()
方法執(zhí)行 cmd 命令并獲取輸出結(jié)果的示例:
import os
# 執(zhí)行 cmd 命令并獲取輸出結(jié)果
output = os.popen("dir").read()
print(output)
請注意,使用 os.system()
和 os.popen()
方法執(zhí)行 cmd 命令可能存在安全風(fēng)險,應(yīng)謹慎使用。如果需要更高級的控制和處理,可以考慮使用 subprocess
模塊。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完