[英]How to execute command prompt and get the output from it?
我是Python的新手,我想編寫一個Python程序,可以在cmd中執行一些命令並自動從中獲取輸出。
可能嗎? 我怎樣才能做到這一點?
您將需要使用subprocess.Popen
:
>>> import subprocess
>>> r = subprocess.Popen(['ls', '-l']) #List files on a linux system. Equivalent of dir on windows.
>>> output, errs = r.communicate()
>>> print(output)
Total 72
# My file list here
Popen
-construtor接受一個參數列表作為第一個參數。 該列表以命令(在本例中為ls
)開頭,其余值是命令的開關和其他參數。 上面的示例在終端(或命令行或控制台)上寫為ls -l
。 相當於一個窗戶
>>> r = subprocess.Popen(['dir', '/A'])
你的意思是如何從cmd使用中執行某些命令
import os
os.system(一個字符串);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.