我需要使用命令 lsmod
来检查是否加载了一个模组,但我不知道运行后如何读取它。我正在使用 subprocess.Popen()
来运行它。在正确方向上的任何一点将不胜感激。 :D
最佳答案
使用 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了解更多详情。
关于python - 阅读命令的反馈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212671/