"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

Published on 2024-11-08
Browse:977

How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

How to Output Child Processes' Results to Files and Terminal Simultaneously in Python

When employing subprocess.call(), it's possible to specify file descriptors as outf and errf to redirect stdout and stderr to specific files. However, these results won't be concurrently displayed in the terminal.

Solution Using Popen and Threading:

To overcome this, we can leverage Popen directly and utilize the stdout=PIPE argument to read from the child process's stdout. Here's how:

import subprocess
from threading import Thread

def tee(infile, *files):
    # Forward output from `infile` to `files` in a separate thread
    def fanout(infile, *files):
        for line in iter(infile.readline, ""):
            for f in files:
                f.write(line)

    t = Thread(target=fanout, args=(infile,)   files)
    t.daemon = True
    t.start()
    return t

def teed_call(cmd_args, **kwargs):
    # Override `stdout` and `stderr` arguments with PIPE to capture standard outputs
    stdout, stderr = [kwargs.pop(s, None) for s in ["stdout", "stderr"]]
    p = subprocess.Popen(
        cmd_args,
        stdout=subprocess.PIPE if stdout is not None else None,
        stderr=subprocess.PIPE if stderr is not None else None,
        **kwargs
    )
    
    # Create threads to simultaneously write to files and terminal
    threads = []
    if stdout is not None:
        threads.append(tee(p.stdout, stdout, sys.stdout))
    if stderr is not None:
        threads.append(tee(p.stderr, stderr, sys.stderr))
        
    # Join the threads to ensure IO completion before proceeding
    for t in threads:
        t.join()

    return p.wait()

Using this function, we can execute child processes and write their output to both files and the terminal at the same time:

outf, errf = open("out.txt", "wb"), open("err.txt", "wb")
teed_call(["cat", __file__], stdout=None, stderr=errf)
teed_call(["echo", "abc"], stdout=outf, stderr=errf, bufsize=0)
teed_call(["gcc", "a b"], close_fds=True, stdout=outf, stderr=errf)
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3