Python subprocess如何将子进程的输出同时写入文件和终端?

20 投票
2 回答
10949 浏览
提问于 2025-04-16 11:44

我正在运行一个脚本,这个脚本会执行多个可执行文件,使用的是

subprocess.call(cmdArgs,stdout=outf, stderr=errf)

outferrf 是 None 或者是文件描述符时(也就是分别为 stdoutstderr 指定不同的文件)。

有没有什么办法可以让每个可执行文件的标准输出和错误输出同时写入文件和终端呢?

2 个回答

0

你可以使用类似这样的东西:https://github.com/waszil/subpiper

在你的回调函数中,你可以随意做任何事情,比如记录日志、写文件、打印等等。它还支持非阻塞模式。

from subpiper import subpiper

def my_stdout_callback(line: str):
    print(f'STDOUT: {line}')

def my_stderr_callback(line: str):
    print(f'STDERR: {line}')

my_additional_path_list = [r'c:\important_location']

retcode = subpiper(cmd='echo magic',
                   stdout_callback=my_stdout_callback,
                   stderr_callback=my_stderr_callback,
                   add_path_list=my_additional_path_list)
28

call()函数其实就是在做一件事情,它等同于先创建一个新的进程,然后等这个进程完成。你也可以直接使用Popen这个方法,并且加上stdout=PIPE这个参数,这样就可以从p.stdout中读取输出了。

#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE
from threading import Thread


def tee(infile, *files):
    """Print `infile` to `files` in a separate thread."""

    def fanout(infile, *files):
        with infile:
            for line in iter(infile.readline, b""):
                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):
    stdout, stderr = [kwargs.pop(s, None) for s in ["stdout", "stderr"]]
    p = Popen(
        cmd_args,
        stdout=PIPE if stdout is not None else None,
        stderr=PIPE if stderr is not None else None,
        **kwargs
    )
    threads = []
    if stdout is not None:
        threads.append(
            tee(p.stdout, stdout, getattr(sys.stdout, "buffer", sys.stdout))
        )
    if stderr is not None:
        threads.append(
            tee(p.stderr, stderr, getattr(sys.stderr, "buffer", sys.stderr))
        )
    for t in threads:
        t.join()  # wait for IO completion
    return p.wait()


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

撰写回答