subprocess.Popen: how to terminate subprocess and receive its output?
<p>I want to run a shell command from python and receive its output with subprocess.Popen. The problem is, when I close the process, sending Ctrl-C, I don't get any output. What am I doing wrong? Code:</p>

<pre><code>&gt;&gt;&gt; import subprocess
&gt;&gt;&gt; sub = subprocess.Popen(["xinput", "test", "8"], stdout=subprocess.PIPE) #receive mouse events
&gt;&gt;&gt; output = sub.communicate()[0].read()
^CTraceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/python2.6/subprocess.py", line 693, in communicate
    stdout = self.stdout.read()
KeyboardInterrupt
&gt;&gt;&gt; output
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'output' is not defined
</code></pre>

<p>Inspired by this post by Jett:</p>

<p><a href="http://stackoverflow.com/questions/12420999/reading-stdout-from-xinput-test-in-python">Reading stdout from xinput test in python</a></p>
