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

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

          <bdo id='SUKNc'></bdo><ul id='SUKNc'></ul>

        <legend id='SUKNc'><style id='SUKNc'><dir id='SUKNc'><q id='SUKNc'></q></dir></style></legend>
        <tfoot id='SUKNc'></tfoot>

        保存子进程命令的错误信息

        Save error message of subprocess command(保存子进程命令的错误信息)

            <tbody id='JwBiE'></tbody>

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

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

              • <bdo id='JwBiE'></bdo><ul id='JwBiE'></ul>
                • <tfoot id='JwBiE'></tfoot>
                  <i id='JwBiE'><tr id='JwBiE'><dt id='JwBiE'><q id='JwBiE'><span id='JwBiE'><b id='JwBiE'><form id='JwBiE'><ins id='JwBiE'></ins><ul id='JwBiE'></ul><sub id='JwBiE'></sub></form><legend id='JwBiE'></legend><bdo id='JwBiE'><pre id='JwBiE'><center id='JwBiE'></center></pre></bdo></b><th id='JwBiE'></th></span></q></dt></tr></i><div id='JwBiE'><tfoot id='JwBiE'></tfoot><dl id='JwBiE'><fieldset id='JwBiE'></fieldset></dl></div>
                  本文介绍了保存子进程命令的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当使用子进程运行 bash 命令时,我可能会遇到命令无效的情况.在这种情况下,bash 将返回错误消息.我们怎样才能捕捉到这个消息?我想将此消息保存到日志文件中.以下是一个示例,我尝试列出不存在目录中的文件.

                  When running bash command using subprocess, I might run into situation where the command is not valid. In this case, bash would return an error messsage. How can we catch this message? I would like to save this message to a log file. The following is an example, where I try to list files in a non-existed directory.

                  try:
                      subprocess.check_call(["ls", "/home/non"]) 
                      df = subprocess.Popen(["ls", "/home/non"], stdout=subprocess.PIPE)        
                      output, err = df.communicate()
                      # process outputs
                  except Exception as error:        
                      print error
                      sys.exit(1)
                  

                  Bash 会打印ls: cannot access/home/non: No such file or directory".我怎样才能得到这个错误信息?except 行捕获的错误明显不同,它说命令'['ls','/home/non']'返回非零退出状态2".

                  Bash would prints "ls: cannot access /home/non: No such file or directory". How can I get this error message? The error caught by the except line is clearly different, it says "Command '['ls', '/home/non']' returned non-zero exit status 2".

                  推荐答案

                  你可以将stderr重定向到一个文件对象:

                  You can redirect stderr to a file object:

                  from subprocess import PIPE, CalledProcessError, check_call, Popen
                  
                  with open("log.txt", "w") as f:
                      try:
                          check_call(["ls", "/home/non"], stderr=f)
                          df = Popen(["ls", "/home/non"], stdout=PIPE)
                          output, err = df.communicate()
                      except CalledProcessError as e:
                          print(e)
                          exit(1)
                  

                  输出到 log.txt:

                  Output to log.txt:

                  ls: cannot access /home/non: No such file or directory
                  

                  如果你想要在except中的消息:

                  If you want the message in the except:

                  try:
                      check_call(["ls", "/home/non"])
                      df = Popen(["ls", "/home/non"], stdout=PIPE)
                      output, err = df.communicate()
                  except CalledProcessError as e:
                      print(e.message)
                  

                  对于 python 2.6,e.message 将不起作用.您可以使用 python 2.7 的 check_output 的类似版本,该版本适用于 python 2.6:

                  For python 2.6 the e.message won't work. You can use a similar version of python 2.7's check_output that will work with python 2.6:

                  from subprocess import PIPE, CalledProcessError, Popen
                  
                  def check_output(*args, **kwargs):
                      process = Popen(stdout=PIPE, *args, **kwargs)
                      out, err = process.communicate()
                      ret = process.poll()
                      if ret:
                          cmd = kwargs.get("args")
                          if cmd is None:
                              cmd = args[0]
                          error = CalledProcessError(ret, cmd)
                          error.out = out
                          error.message = err
                          raise error
                      return out
                  
                  try:
                      out = check_output(["ls", "/home"], stderr=PIPE)
                      df = Popen(["ls", "/home/non"], stdout=PIPE)
                      output, err = df.communicate()
                  except CalledProcessError as e:
                      print(e.message)
                  else:
                      print(out)
                  

                  这篇关于保存子进程命令的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                • <small id='nMziv'></small><noframes id='nMziv'>

                    <bdo id='nMziv'></bdo><ul id='nMziv'></ul>

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

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