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

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

      3. python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令

        python check_output fails with exit status 1 but Popen works for same command(python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令)
            <bdo id='xLCzS'></bdo><ul id='xLCzS'></ul>
              <tbody id='xLCzS'></tbody>

              <legend id='xLCzS'><style id='xLCzS'><dir id='xLCzS'><q id='xLCzS'></q></dir></style></legend>
            1. <small id='xLCzS'></small><noframes id='xLCzS'>

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

                • 本文介绍了python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  用于识别 Xcode 是否在 Mac 上运行的命令框架:cmd = "ps -ax | grep -v grep | grep Xcode"

                  Command framed to identify if Xcode is running on Mac: cmd = "ps -ax | grep -v grep | grep Xcode"

                  如果 Xcode 没有运行,那么上面的命令适用于 subprocess 模块的 Popen 方法,但会引发 CalledProcessErrorcheck_output 方法.我试图通过以下代码检查stderr,但未能获得适当的信息来理解原因.

                  If Xcode is not running, then above command works well with Popen method of subprocess module, but raises a CalledProcessError with check_output method. I tried to inspect the stderr through the following code, but failed to get appropriate information to understand the reason.

                  from subprocess import check_output, STDOUT, CalledProcessError
                  
                  psCmd = "ps -ax | grep -v grep | grep Xcode"
                  o = None
                  try:
                      o = check_output(psCmd, stderr=STDOUT, shell=True)
                  except CalledProcessError as ex:
                      print 'Error:', ex, o
                  

                  异常信息如下:

                  错误:命令'ps -ax |grep -v grep |grep Xcode' 返回非零退出状态 1 无

                  问题:为什么上面的命令对 Popen 有效,但由于 check_output 失败?

                  Question: Why the above command works with Popen, but fails with check_output ?

                  注意:如果 Xcode 正在运行,命令对这两种方法都适用.

                  Note: Command works well with both approach, if Xcode is running.

                  推荐答案

                  check_output() 按预期工作.下面是它在 Popen() 方面的简化实现:

                  check_output() works as expected. Here's its simplified implementation in terms of Popen():

                  def check_output(cmd):
                      process = Popen(cmd, stdout=PIPE)
                      output = process.communicate()[0]
                      if process.returncode != 0:
                          raise CalledProcessError(process.returncode, cmd, output=output)
                      return output
                  

                  grep 如果没有找到任何东西,则返回 1,也就是说,如果 Xcode 没有运行,您应该期待异常.

                  grep returns 1 if it hasn't found anything i.e., you should expect the exception if Xcode is not running.

                  注意:如实现所示,即使发生异常也能得到输出:

                  Note: as the implementation shows, you can get the output even if the exception occurs:

                  #!/usr/bin/env python
                  from subprocess import check_output, STDOUT, CalledProcessError
                  
                  cmd = "ps -ax | grep -v grep | grep Xcode"
                  try:
                      o = check_output(cmd, stderr=STDOUT, shell=True)
                      returncode = 0
                  except CalledProcessError as ex:
                      o = ex.output
                      returncode = ex.returncode
                      if returncode != 1: # some other error happened
                          raise
                  

                  您可以改用 pgrep -a Xcode 命令(注意:以 p 开头)或使用 psutil 模块作为可移植代码:

                  You could probably use pgrep -a Xcode command instead (note: starts with p) or use psutil module for a portable code:

                  #!/usr/bin/env python
                  import psutil # $ pip install psutil
                  
                  print([p.as_dict() for p in psutil.process_iter() if 'Xcode' in p.name()])
                  

                  这篇关于python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

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

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