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

      <bdo id='UlE0b'></bdo><ul id='UlE0b'></ul>
  1. <tfoot id='UlE0b'></tfoot>

  2. <small id='UlE0b'></small><noframes id='UlE0b'>

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

      “子进程.Popen"- 检查成功和错误

      时间:2023-07-22
      • <bdo id='jPSS5'></bdo><ul id='jPSS5'></ul>
        <tfoot id='jPSS5'></tfoot>
        <i id='jPSS5'><tr id='jPSS5'><dt id='jPSS5'><q id='jPSS5'><span id='jPSS5'><b id='jPSS5'><form id='jPSS5'><ins id='jPSS5'></ins><ul id='jPSS5'></ul><sub id='jPSS5'></sub></form><legend id='jPSS5'></legend><bdo id='jPSS5'><pre id='jPSS5'><center id='jPSS5'></center></pre></bdo></b><th id='jPSS5'></th></span></q></dt></tr></i><div id='jPSS5'><tfoot id='jPSS5'></tfoot><dl id='jPSS5'><fieldset id='jPSS5'></fieldset></dl></div>

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

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

                  <tbody id='jPSS5'></tbody>
              1. 本文介绍了“子进程.Popen"- 检查成功和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想检查一个子进程是成功完成还是失败了.目前我已经提出了一个解决方案,但我不确定它是否正确和可靠.是否保证每个进程仅将其错误输出到 stderr 尊重 stdout:

                I want to check if a subprocess has finished execution successfully or failed. Currently I have come up with a solution but I am not sure if it is correct and reliable. Is it guaranteed that every process outputs its errors only to stderr respectfully to stdout:

                注意:我对重定向/打印输出不感兴趣.我已经知道该怎么做了.

                Note: I am not interested in just redirecting/printing out the output. That I know already how to do.

                pipe = subprocess.Popen(command,
                                                stdout=subprocess.PIPE,
                                                stderr=subprocess.PIPE,
                                                universal_newlines=True)
                
                if "" == pipe.stdout.readline():
                    print("Success")
                    self.isCommandExectutionSuccessful = True
                
                if not "" == pipe.stderr.readline():
                    print("Error")
                    self.isCommandExectutionSuccessful = True
                

                或者:

                   if "" == pipe.stdout.readline():
                       print("Success")
                       self.isCommandExectutionSuccessful = True
                   else:
                       print("Error")
                       self.isCommandExectutionSuccessful = False
                

                和:

                   if not "" == pipe.stderr.readline():
                       print("Success")
                       self.isCommandExectutionSuccessful = True
                   else:
                       print("Error")
                       self.isCommandExectutionSuccessful = False
                

                推荐答案

                你需要对进程的输出做些什么吗?

                Do you need to do anything with the output of the process?

                check_call 方法在这里可能很有用.在此处查看 python 文档:https://docs.python.org/2/library/subprocess.html#subprocess.check_call

                The check_call method might be useful here. See the python docs here: https://docs.python.org/2/library/subprocess.html#subprocess.check_call

                然后您可以按如下方式使用它:

                You can then use this as follows:

                try:
                  subprocess.check_call(command)
                except subprocess.CalledProcessError:
                  # There was an error - command exited with non-zero code
                

                但是,这依赖于 command 返回退出代码 0 表示成功完成,返回非零值表示错误.

                However, this relies on command returning an exit code of 0 for succesful completion and a non-zero value for an error.

                如果您还需要捕获输出,那么 check_output 方法可能更合适.如果您也需要,仍然可以重定向标准错误.

                If you need to capture the output as well, then the check_output method may be more appropriate. It is still possible to redirect the standard error if you need this as well.

                try:
                  proc = subprocess.check_output(command, stderr=subprocess.STDOUT)
                  # do something with output
                except subprocess.CalledProcessError:
                  # There was an error - command exited with non-zero code
                

                在此处查看文档:https://docs.python.org/2/library/subprocess.html#subprocess.check_output

                这篇关于“子进程.Popen"- 检查成功和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:什么会导致“IOError: [Errno 9] Bad file descriptor"?在 os.syst 下一篇:在 Python 中运行 BASH 内置命令?

                相关文章

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

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