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

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

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

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

      2. 返回标准输出内容的 subprocess.check_call 有什么好的等价物?

        What#39;s a good equivalent to subprocess.check_call that returns the contents of stdout?(返回标准输出内容的 subprocess.check_call 有什么好的等价物?)
            <tbody id='yWiWF'></tbody>

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

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

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

                • <bdo id='yWiWF'></bdo><ul id='yWiWF'></ul>
                  本文介绍了返回标准输出内容的 subprocess.check_call 有什么好的等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一个与 subprocess.check_call 的接口相匹配的好方法——即,它在失败时抛出 CalledProcessError,是同步的,&c -- 但不是返回命令的返回码(如果它甚至这样做的话)返回程序的输出,要么只是标准输出,要么是(stdout,stderr)的元组.

                  I'd like a good method that matches the interface of subprocess.check_call -- ie, it throws CalledProcessError when it fails, is synchronous, &c -- but instead of returning the return code of the command (if it even does that) returns the program's output, either only stdout, or a tuple of (stdout, stderr).

                  有人有这样的方法吗?

                  推荐答案

                  Python 2.7+

                  from subprocess import check_output as qx
                  

                  Python

                  2.7

                  来自 subprocess.py:

                  import subprocess
                  def check_output(*popenargs, **kwargs):
                      if 'stdout' in kwargs:
                          raise ValueError('stdout argument not allowed, it will be overridden.')
                      process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
                      output, unused_err = process.communicate()
                      retcode = process.poll()
                      if retcode:
                          cmd = kwargs.get("args")
                          if cmd is None:
                              cmd = popenargs[0]
                          raise subprocess.CalledProcessError(retcode, cmd, output=output)
                      return output
                  
                  class CalledProcessError(Exception):
                      def __init__(self, returncode, cmd, output=None):
                          self.returncode = returncode
                          self.cmd = cmd
                          self.output = output
                      def __str__(self):
                          return "Command '%s' returned non-zero exit status %d" % (
                              self.cmd, self.returncode)
                  # overwrite CalledProcessError due to `output` keyword might be not available
                  subprocess.CalledProcessError = CalledProcessError
                  

                  另请参阅将系统命令输出捕获为字符串 另一个可能的 check_output() 实现示例.

                  See also Capturing system command output as a string for another example of possible check_output() implementation.

                  这篇关于返回标准输出内容的 subprocess.check_call 有什么好的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='hwnBE'></tfoot>
                    <legend id='hwnBE'><style id='hwnBE'><dir id='hwnBE'><q id='hwnBE'></q></dir></style></legend>

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

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