Pythonからシェルコマンドを実行し、subprocess.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によるこの投稿に触発されました: