• <legend id='N8wgQ'><style id='N8wgQ'><dir id='N8wgQ'><q id='N8wgQ'></q></dir></style></legend>

    1. <tfoot id='N8wgQ'></tfoot>

          <bdo id='N8wgQ'></bdo><ul id='N8wgQ'></ul>
      1. <small id='N8wgQ'></small><noframes id='N8wgQ'>

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

        将 subprocess.Popen 输出附加到文件?

        append subprocess.Popen output to file?(将 subprocess.Popen 输出附加到文件?)

            <tbody id='1uytv'></tbody>

            <bdo id='1uytv'></bdo><ul id='1uytv'></ul>

          • <small id='1uytv'></small><noframes id='1uytv'>

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

                1. <tfoot id='1uytv'></tfoot>
                  本文介绍了将 subprocess.Popen 输出附加到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我可以成功地将我的输出重定向到一个文件,但这似乎覆盖了文件的现有数据:

                  I can successfully redirect my output to a file, however this appears to overwrite the file's existing data:

                  import subprocess
                  outfile = open('test','w') #same with "w" or "a" as opening mode
                  outfile.write('Hello')
                  subprocess.Popen('ls',stdout=outfile)
                  

                  将从文件中删除 'Hello' 行.

                  will remove the 'Hello' line from the file.

                  我想一种解决方法是将输出作为字符串或其他东西存储在其他地方(不会太长),然后用 outfile.write(thestring) 手动附加它 - 但我是想知道我是否在模块中遗漏了一些有助于实现这一点的东西.

                  I guess a workaround is to store the output elsewhere as a string or something (it won't be too long), and append this manually with outfile.write(thestring) - but I was wondering if I am missing something within the module that facilitates this.

                  推荐答案

                  你当然可以将 subprocess.Popen 的输出附加到一个文件中,我每天都在使用它.这是我的做法:

                  You sure can append the output of subprocess.Popen to a file, and I make a daily use of it. Here's how I do it:

                  log = open('some file.txt', 'a')  # so that data written to it will be appended
                  c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
                  

                  (当然,这是一个虚拟的例子,我没有使用 subprocess 来列出文件...)

                  (of course, this is a dummy example, I'm not using subprocess to list files...)

                  顺便说一句,其他行为类似于文件的对象(特别是 write() 方法)可以替换此 log 项,因此您可以缓冲输出,并执行随心所欲(写入文件、显示等)[但这似乎并不容易,请参阅下面的评论].

                  By the way, other objects behaving like file (with write() method in particular) could replace this log item, so you can buffer the output, and do whatever you want with it (write to file, display, etc) [but this seems not so easy, see my comment below].

                  注意:可能具有误导性的是,subprocess 出于某种我不明白的原因,会在您想要编写的内容之前编写.所以,这是使用它的方法:

                  Note: what may be misleading, is the fact that subprocess, for some reason I don't understand, will write before what you want to write. So, here's the way to use this:

                  log = open('some file.txt', 'a')
                  log.write('some text, as header of the file
                  ')
                  log.flush()  # <-- here's something not to forget!
                  c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
                  

                  所以提示是:不要忘记flush输出!

                  So the hint is: do not forget to flush the output!

                  这篇关于将 subprocess.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 画布滚动条?)

                    1. <small id='oeCZa'></small><noframes id='oeCZa'>

                        <tbody id='oeCZa'></tbody>

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

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

                          <bdo id='oeCZa'></bdo><ul id='oeCZa'></ul>
                          1. <legend id='oeCZa'><style id='oeCZa'><dir id='oeCZa'><q id='oeCZa'></q></dir></style></legend>