Skip to content

Instantly share code, notes, and snippets.

@zhenyi2697
Created October 30, 2013 09:12
Show Gist options
  • Save zhenyi2697/7229421 to your computer and use it in GitHub Desktop.
Save zhenyi2697/7229421 to your computer and use it in GitHub Desktop.
Python: continuously read content from subprocess
#!/usr/bin/python
import subprocess
import time
cmd = 'python ./output.py'
proc = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
output = ''
while True:
result = proc.poll()
delta = proc.stdout.read(1)
if result is not None:
print 'terminated'
break
if delta != ' ':
output = output + delta
else:
if '%' in output:
print 'percentage is:'
print output
elif '/s' in output:
print 'speed is:'
print output
print 'output is:'
print output
output = ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment