Realtime output from a shell command in Python

2015-07-02 · 2 min read

Running a shell command in Python usually waits until the process is finished and only then sends its entire output. In the following example it is process.communicate() that blocks till given command is completed.

import subprocess
import shlex

command = shlex.split("ping -c 5")
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, err = process.communicate()
print output

subprocess module allows to capture realtime output as well - here's how:

from subprocess import Popen, PIPE

def run(command):
    process = Popen(command, stdout=PIPE, shell=True)
    while True:
        line = process.stdout.readline().rstrip()
        if not line:
        yield line

if __name__ == "__main__":
    for path in run("ping -c 5"):
        print path

Subscribe to My Newsletter

The latest programming-related news, articles and resources - sent to your inbox monthly. Unsubscribe anytime.