• <tfoot id='iTjZB'></tfoot>

      1. <i id='iTjZB'><tr id='iTjZB'><dt id='iTjZB'><q id='iTjZB'><span id='iTjZB'><b id='iTjZB'><form id='iTjZB'><ins id='iTjZB'></ins><ul id='iTjZB'></ul><sub id='iTjZB'></sub></form><legend id='iTjZB'></legend><bdo id='iTjZB'><pre id='iTjZB'><center id='iTjZB'></center></pre></bdo></b><th id='iTjZB'></th></span></q></dt></tr></i><div id='iTjZB'><tfoot id='iTjZB'></tfoot><dl id='iTjZB'><fieldset id='iTjZB'></fieldset></dl></div>

        <small id='iTjZB'></small><noframes id='iTjZB'>

        • <bdo id='iTjZB'></bdo><ul id='iTjZB'></ul>
        <legend id='iTjZB'><style id='iTjZB'><dir id='iTjZB'><q id='iTjZB'></q></dir></style></legend>

        Python子进程将孩子的输出输出到文件和终端?

        Python subprocess get children#39;s output to file and terminal?(Python子进程将孩子的输出输出到文件和终端?)
        <legend id='23OtM'><style id='23OtM'><dir id='23OtM'><q id='23OtM'></q></dir></style></legend>
        1. <tfoot id='23OtM'></tfoot>

            <i id='23OtM'><tr id='23OtM'><dt id='23OtM'><q id='23OtM'><span id='23OtM'><b id='23OtM'><form id='23OtM'><ins id='23OtM'></ins><ul id='23OtM'></ul><sub id='23OtM'></sub></form><legend id='23OtM'></legend><bdo id='23OtM'><pre id='23OtM'><center id='23OtM'></center></pre></bdo></b><th id='23OtM'></th></span></q></dt></tr></i><div id='23OtM'><tfoot id='23OtM'></tfoot><dl id='23OtM'><fieldset id='23OtM'></fieldset></dl></div>

                <tbody id='23OtM'></tbody>
                <bdo id='23OtM'></bdo><ul id='23OtM'></ul>

                • <small id='23OtM'></small><noframes id='23OtM'>

                  本文介绍了Python子进程将孩子的输出输出到文件和终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm running a script that executes a number of executables by using

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

                  when outf/errf is either None or a file descriptor (different files for stdout/stderr).

                  Is there any way I can execute each exe so that the stdout and stderr will be written to the files and terminal together?

                  解决方案

                  The call() function is just Popen(*args, **kwargs).wait(). You could call Popen directly and use stdout=PIPE argument to read from 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)
                  

                  这篇关于Python子进程将孩子的输出输出到文件和终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                • <tfoot id='yhHJS'></tfoot>

                  <small id='yhHJS'></small><noframes id='yhHJS'>

                      <tbody id='yhHJS'></tbody>
                        <bdo id='yhHJS'></bdo><ul id='yhHJS'></ul>

                        <i id='yhHJS'><tr id='yhHJS'><dt id='yhHJS'><q id='yhHJS'><span id='yhHJS'><b id='yhHJS'><form id='yhHJS'><ins id='yhHJS'></ins><ul id='yhHJS'></ul><sub id='yhHJS'></sub></form><legend id='yhHJS'></legend><bdo id='yhHJS'><pre id='yhHJS'><center id='yhHJS'></center></pre></bdo></b><th id='yhHJS'></th></span></q></dt></tr></i><div id='yhHJS'><tfoot id='yhHJS'></tfoot><dl id='yhHJS'><fieldset id='yhHJS'></fieldset></dl></div>

                            <legend id='yhHJS'><style id='yhHJS'><dir id='yhHJS'><q id='yhHJS'></q></dir></style></legend>