2013-04-25 59 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了解更多详情。