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:
<code class="python">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()</code>
Using this function, we can execute child processes and write their output to both files and the terminal at the same time:
<code class="python">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)</code>
The above is the detailed content of How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?. For more information, please follow other related articles on the PHP Chinese website!