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

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

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

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

        程序完成后如何将控制台打印到文本文件(Python)?

        How to print the console to a text file AFTER the program finishes (Python)?(程序完成后如何将控制台打印到文本文件(Python)?)

          <legend id='Qnr6n'><style id='Qnr6n'><dir id='Qnr6n'><q id='Qnr6n'></q></dir></style></legend>
            <bdo id='Qnr6n'></bdo><ul id='Qnr6n'></ul>
            <tfoot id='Qnr6n'></tfoot>

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

                  <tbody id='Qnr6n'></tbody>

                  <i id='Qnr6n'><tr id='Qnr6n'><dt id='Qnr6n'><q id='Qnr6n'><span id='Qnr6n'><b id='Qnr6n'><form id='Qnr6n'><ins id='Qnr6n'></ins><ul id='Qnr6n'></ul><sub id='Qnr6n'></sub></form><legend id='Qnr6n'></legend><bdo id='Qnr6n'><pre id='Qnr6n'><center id='Qnr6n'></center></pre></bdo></b><th id='Qnr6n'></th></span></q></dt></tr></i><div id='Qnr6n'><tfoot id='Qnr6n'></tfoot><dl id='Qnr6n'><fieldset id='Qnr6n'></fieldset></dl></div>
                  本文介绍了程序完成后如何将控制台打印到文本文件(Python)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个程序通过打印语句将许多计算和结果输出到控制台.我想编写一些代码来将控制台的所有内容导出(或保存)到一个简单的文本文件中.

                  I have a program that outputs many calculations and results to the console through the print statement. I want to write some code to export (or save) all the contents of the console to a simple text file.

                  我搜索了 StackOverflow 和其他网站,但我发现了一些方法可以将打印语句重定向到直接打印到文件,但我希望程序正常工作,将输出显示到控制台,然后在所有操作之后保存其内容程序完成.

                  I searched StackOverflow and other sites but I found some methods to redirect the print statement to print to a file directly, but I want the program to work normally, to display outputs to the console, then to save its contents AFTER all operations of the program done.

                  如果重要的话,我将 PyCharm 与 Python2.7 一起使用

                  I am using PyCharm with Python2.7 if it matters

                  推荐答案

                  非常感谢和尊重所有为这个问题做出贡献的人.我终于找到了解决这个问题的方法,只需对我的原始代码进行最少的修改.解决方案由成员@Status 提供,这里是它的链接.

                  With all thanks and respect to all who contributed to this question. I have finally found a solution to this problem with minimal modifications to my original code. The solution is provided by the member @Status and here is its link .

                  虽然我在发布我的问题之前进行了很多搜索,但尊敬的成员的回答启发了我进行精确搜索的思路,尤其是执行出色工作的@turkus 和让我大开眼界的@Glostas 的贡献"tee",它引导我找到我发布的解决方案(尽管它不包含tee").

                  Although I searched a lot before posting my question, but the answers of the respected members enlightened my mind to a precise search especially the contributions of @turkus, who performs an exceptional work, and @Glostas who opened my eyes to the "tee" which guided me to find the solution I posted (although it does not contain "tee").

                  解决方案上述帖子略有修改em>:

                  The solution, as of the mentioned post with slight modifications:

                  1- 将以下类放入程序中:

                  1- Put the following Class in the program:

                  class Logger(object):
                  """
                  Lumberjack class - duplicates sys.stdout to a log file and it's okay
                  source: https://stackoverflow.com/a/24583265/5820024
                  """
                  def __init__(self, filename="Red.Wood", mode="a", buff=0):
                      self.stdout = sys.stdout
                      self.file = open(filename, mode, buff)
                      sys.stdout = self
                  
                  def __del__(self):
                      self.close()
                  
                  def __enter__(self):
                      pass
                  
                  def __exit__(self, *args):
                      pass
                  
                  def write(self, message):
                      self.stdout.write(message)
                      self.file.write(message)
                  
                  def flush(self):
                      self.stdout.flush()
                      self.file.flush()
                      os.fsync(self.file.fileno())
                  
                  def close(self):
                      if self.stdout != None:
                          sys.stdout = self.stdout
                          self.stdout = None
                  
                      if self.file != None:
                          self.file.close()
                          self.file = None
                  

                  2- 在程序的开头,在任何打印语句之前,放这一行:

                  2- At the beginning of the program, before any print statements, put this line:

                  my_console = Logger('my_console_file.txt')  # you can change the file's name
                  

                  3- 在程序的最后,在所有的 print 语句之后,放入这一行:

                  3- At the end of the program, after all of the print statements, put this line:

                  my_console.close()
                  

                  我对此进行了测试,它运行良好,最后我在程序结束后克隆了控制台的输出.

                  I tested this, and It works perfectly, and finally I have a clone of the console's output after the program ends.

                  向大家致以最诚挚的问候,并非常感谢所有贡献者.

                  With best regards to everybody, and Many thanks to all contributors.

                  这篇关于程序完成后如何将控制台打印到文本文件(Python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  <tfoot id='j752Q'></tfoot>
                    <i id='j752Q'><tr id='j752Q'><dt id='j752Q'><q id='j752Q'><span id='j752Q'><b id='j752Q'><form id='j752Q'><ins id='j752Q'></ins><ul id='j752Q'></ul><sub id='j752Q'></sub></form><legend id='j752Q'></legend><bdo id='j752Q'><pre id='j752Q'><center id='j752Q'></center></pre></bdo></b><th id='j752Q'></th></span></q></dt></tr></i><div id='j752Q'><tfoot id='j752Q'></tfoot><dl id='j752Q'><fieldset id='j752Q'></fieldset></dl></div>

                      <tbody id='j752Q'></tbody>
                    <legend id='j752Q'><style id='j752Q'><dir id='j752Q'><q id='j752Q'></q></dir></style></legend>
                        <bdo id='j752Q'></bdo><ul id='j752Q'></ul>

                      • <small id='j752Q'></small><noframes id='j752Q'>