我想从python运行一个shell命令,并使用子流程.Popen. 问题是,当我关闭进程并发送Ctrl-C时,我没有得到任何输出。我做错什么了?代码:
>>> import subprocess
>>> sub = subprocess.Popen(["xinput", "test", "8"], stdout=subprocess.PIPE) #receive mouse events
>>> output = sub.communicate()[0].read()
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 693, in communicate
stdout = self.stdout.read()
keyboardInterrupt
>>> output
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'output' is not defined
灵感来自Jett的这篇文章:
@pythonm已经解释了
NameError
。在此外,您使用
Popen.communicate()
的输出在概念上是错误的。它返回一个2元组的字符串:(stdout, stderr)
。它不返回两个类似文件的对象。这就是为什么如果communicate()
返回,sub.communicate()[0].read()
会失败。在在子进程返回之前,})。只有在子进程终止后,您才有权访问
communicate()
聚合其所有的stdout和stderr(考虑到您向构造函数提供了stdout=subprocess.PIPE
和{communicate()
在子进程运行时收集的内容。在如果您想实时监视子进程的输出,那么
communicate()
是错误的方法。运行子进程,监视它(例如在一个循环中),并与其Popen.stdout
和Popen.stderr
属性(它们那时是类似文件的对象)交互。@mgilson的回答向您展示了一种方法:)这里的问题是
KeyboardInterrupt
是在调用communicate
时发送的。结果,communicate
永远不会返回,因此它的输出永远不会存储在变量output
中,当您尝试使用它时,您将得到NameError
。一种解决方法是:相关问题 更多 >
编程相关推荐