daily pastebin goal
76%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. >>> import subprocess
  2. >>> sub = subprocess.Popen(["xinput", "test", "8"], stdout=subprocess.PIPE) #receive mouse events
  3. >>> output = sub.communicate()[0].read()
  4. ^CTraceback (most recent call last):
  5.   File "<stdin>", line 1, in <module>
  6.   File "/usr/lib/python2.6/subprocess.py", line 693, in communicate
  7.     stdout = self.stdout.read()
  8. KeyboardInterrupt
  9. >>> output
  10. Traceback (most recent call last):
  11.   File "<stdin>", line 1, in <module>
  12. NameError: name 'output' is not defined
  13.    
  14. import subprocess
  15.  sub = subprocess.Popen(["xinput", "test", "8"], stdout=subprocess.PIPE)
  16.  lines = [] #Need someplace to store the data as it comes
  17.  try:
  18.     for line in sub.stdout: #read one line from standard out, store it in lines
  19.         lines.append(line)
  20.  except KeyboardInterrupt:  #keyboardInterrupt happened.  Stop process
  21.     sub.terminate()
  22.  finally:                   #Join our lines into a single buffer (like `communicate`)
  23.     output = ''.join(lines)
  24.     del lines               #clean `lines` out of our namespace (just because).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top