How to get live output from the subprocess command with Python?

Sometimes, we want to get live output from the subprocess command with Python.

In this article, we’ll look at how to get live output from the subprocess command with Python.

How to get live output from the subprocess command with Python?

To get live output from the subprocess command with Python, we can open a file with open and write to it with write.

And sys.stdout.buffer.write will write the output to the screen.

For instance, we write:

import subprocess
import sys
with open('test.log', 'wb') as f:
    process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
    for c in iter(lambda: process.stdout.read(1), b''):
        sys.stdout.buffer.write(c)
        f.write(c)

We open the file with open.

Then we call subprocess.POpen with the command and argument in a list and set stdout to subprocess.PIPE.

Next, we loop through the iterator that we get from call iter with process.stdout.read(1) to read the live output and loop through them.

In the loop body, we call sys.stdout.buffer.write to write the output to the screen and f.write to write the output to test.log

Therefore, we get something like:

total 20
-rw-r--r-- 1 runner runner   19 Oct 22 23:52 foo.csv
-rw-r--r-- 1 runner runner  242 Oct 23 16:45 main.py
-rw-r--r-- 1 runner runner    4 Oct 23 16:24 out.txt
-rw-r--r-- 1 runner runner 1473 Oct 23 16:28 poetry.lock
-rw-r--r-- 1 runner runner  312 Oct 23 16:28 pyproject.toml
-rw-r--r-- 1 runner runner    0 Oct 23 16:52 test.log

written to the screen and to the file.

Conclusion

To get live output from the subprocess command with Python, we can open a file with open and write to it with write.

And sys.stdout.buffer.write will write the output to the screen.