Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子进程获取子进程';输出到文件和终端?_Python_Subprocess_Parent - Fatal编程技术网

Python子进程获取子进程';输出到文件和终端?

Python子进程获取子进程';输出到文件和终端?,python,subprocess,parent,Python,Subprocess,Parent,我正在运行一个脚本,通过使用 subprocess.call(cmdArgs,stdout=outf, stderr=errf) 当outf/errf为无或文件描述符时(不同的文件用于stdout/stderr) 是否有任何方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?该函数仅适用于。您可以直接调用Popen,并使用stdout=PIPE参数读取p.stdout: #!/usr/bin/env python import sys from subprocess i

我正在运行一个脚本,通过使用

subprocess.call(cmdArgs,stdout=outf, stderr=errf)
outf
/
errf
为无或文件描述符时(不同的文件用于
stdout
/
stderr

是否有任何方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?

该函数仅适用于。您可以直接调用
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)

您可以使用以下内容:

在您的回调中,您可以做任何您喜欢的事情,记录、写入文件、打印等。它还支持非阻塞模式

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)

感谢您的快速回复,但它不起作用。外部进程只看到操作系统级的文件句柄(从文件对象的fileno()方法中获得的数字)。请参阅,谢谢,如果我不想使用subprocess.Popen(而不是Call)运行多个exec,而想使用subprocess.Popen(而不是Call)来代替subprocess.Call,您会怎么做terminal@user515766:解决方案相同:将
stdout
stderr
设置为
PIPE
并调用
tee()
当您想向多个地方写信时。有人删除了表明第一条评论(“无效”)错误的评论。它混淆了
子进程.call
和现在被调用的
teed_call
函数
call
(不同),以避免歧义。@Peilonrayz:我已使答案中的代码与Python 2/3兼容。(这是2011年的纯Python2解决方案)可能与我使用cmd='python3 run2.py'尝试过的版本相同,但出现了一个错误。我是否缺少如何将run2.py作为参数发送的信息:FileNotFoundError:[Errno 2]没有这样的文件或目录:“python3 run2.py”:“python3 run2.py”