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

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

    • <bdo id='UPMoH'></bdo><ul id='UPMoH'></ul>

    <tfoot id='UPMoH'></tfoot>

      1. <i id='UPMoH'><tr id='UPMoH'><dt id='UPMoH'><q id='UPMoH'><span id='UPMoH'><b id='UPMoH'><form id='UPMoH'><ins id='UPMoH'></ins><ul id='UPMoH'></ul><sub id='UPMoH'></sub></form><legend id='UPMoH'></legend><bdo id='UPMoH'><pre id='UPMoH'><center id='UPMoH'></center></pre></bdo></b><th id='UPMoH'></th></span></q></dt></tr></i><div id='UPMoH'><tfoot id='UPMoH'></tfoot><dl id='UPMoH'><fieldset id='UPMoH'></fieldset></dl></div>
      2. 如何使用 Popen 同时写入标准输出和记录文件?

        How to write to stdout AND to log file simultaneously with Popen?(如何使用 Popen 同时写入标准输出和记录文件?)

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

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

                    <tbody id='ReImn'></tbody>
                • <small id='ReImn'></small><noframes id='ReImn'>

                  本文介绍了如何使用 Popen 同时写入标准输出和记录文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Popen 调用一个不断将其标准输出和标准错误写入日志文件的 shell 脚本.有没有办法同时连续输出日志文件(到屏幕上),或者让shell脚本同时写入日志文件和stdout?

                  I am using Popen to call a shell script that is continuously writing its stdout and stderr to a log file. Is there any way to simultaneously output the log file continuously (to the screen), or alternatively, make the shell script write to both the log file and stdout at the same time?

                  我基本上想在 Python 中做这样的事情:

                  I basically want to do something like this in Python:

                  cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
                  

                  同样,这将 stderr/stdout 一起传送到 tee,tee 将其同时写入 stdout 和我的日志文件.

                  Again, this pipes stderr/stdout together to tee, which writes it both to stdout and my logfile.

                  我知道如何在 Python 中将 stdout 和 stderr 写入日志文件.我被困的地方是如何将这些复制回屏幕:

                  I know how to write stdout and stderr to a logfile in Python. Where I'm stuck is how to duplicate these back to the screen:

                  subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
                  

                  当然,我可以做这样的事情,但是有没有办法在没有 tee 和 shell 文件描述符重定向的情况下做到这一点?:

                  Of course, I could just do something like this, but is there any way to do this without tee and shell file descriptor redirection?:

                  subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
                  

                  推荐答案

                  你可以使用管道从程序的stdout中读取数据,并将其写入你想要的所有地方:

                  You can use a pipe to read the data from the program's stdout and write it to all the places you want:

                  import sys
                  import subprocess
                  
                  logfile = open('logfile', 'w')
                  proc=subprocess.Popen(['cat', 'file'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                  for line in proc.stdout:
                      sys.stdout.write(line)
                      logfile.write(line)
                  proc.wait()
                  

                  更新

                  在 python 3 中,universal_newlines 参数控制管道的使用方式.如果 False,管道读取返回 bytes 对象并且可能需要被解码(例如,line.decode('utf-8'))得到一个字符串.如果 True,python 会为你解码

                  In python 3, the universal_newlines parameter controls how pipes are used. If False, pipe reads return bytes objects and may need to be decoded (e.g., line.decode('utf-8')) to get a string. If True, python does the decode for you

                  在 3.3 版中更改:当 universal_newlines 为 True 时,该类使用编码 locale.getpreferredencoding(False) 而不是 locale.getpreferredencoding().有关此更改的更多信息,请参阅 io.TextIOWrapper 类.

                  Changed in version 3.3: When universal_newlines is True, the class uses the encoding locale.getpreferredencoding(False) instead of locale.getpreferredencoding(). See the io.TextIOWrapper class for more information on this change.

                  这篇关于如何使用 Popen 同时写入标准输出和记录文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)

                        <small id='4JpAj'></small><noframes id='4JpAj'>

                        <legend id='4JpAj'><style id='4JpAj'><dir id='4JpAj'><q id='4JpAj'></q></dir></style></legend>

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