2013-04-25 62 views
0

我需要使用命令lsmod來檢查模裝,但我不知道如何從運行它之後讀取。即時通訊使用subprocess.Popen()來運行它。任何正確的方向將非常感激。 :d閱讀命令的反饋?

回答

0

假設你是在lsmod尋找ath,然後命令將是:lsmod | grep ath

使用subprocess

In [60]: c=subprocess.Popen("lsmod",stdout=subprocess.PIPE) 

In [61]: gr=subprocess.Popen(["grep" ,"ath"],stdin=c.stdout,stdout=subprocess.PIPE) 

In [62]: print gr.communicate()[0] 
ath5k     135206 0 
ath     19188 1 ath5k 
mac80211    461261 1 ath5k 
cfg80211    175574 3 ath5k,ath,mac80211 
+0

我不會用'這裏grep',而是通過自己搜索。 – glglgl 2013-04-25 11:00:27

2

使用subprocess.Popen(stdout=subprocess.PIPE),然後調用subprocess.communicate()讀取輸出。基本用法:

process = subprocess.Popen(['lsmod'], stdout=subprocess.PIPE) # Can also capture stderr 
result_str = process.communicate()[0] # Or [1] for stderr 

請參閱the Python documentation瞭解更多詳情。