【问题标题】:"subprocess.Popen" - checking for success and errors“subprocess.Popen” - 检查成功和错误
【发布时间】:2014-09-24 14:13:42
【问题描述】:

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

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

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

【问题讨论】:

    标签: python python-3.x subprocess stdout stderr


    【解决方案1】:

    这就是我最终做到的:

        # Call a system process
        try:
            # universal_newlines - makes manual decoding of subprocess.stdout unnecessary
            output = subprocess.check_output(command,
                                             stderr=subprocess.STDOUT,
                                             universal_newlines=True)
    
            # Print out command's standard output (elegant)
            self.textEdit_CommandLineOutput.insertPlainText(output)
            self.isCommandExecutionSuccessful = True
    
        except subprocess.CalledProcessError as error:
            self.isCommandExecutionSuccessful = False
    
            errorMessage = ">>> Error while executing:\n"\
                           + command\
                           + "\n>>> Returned with error:\n"\
                           + str(error.output)
            self.textEdit_CommandLineOutput.append(errorMessage)
    
            QMessageBox.critical(None,
                                 "ERROR",
                                 errorMessage)
            print("Error: " + errorMessage)
    
        except FileNotFoundError as error:
            errorMessage = error.strerror
            QMessageBox.critical(None,
                                 "ERROR",
                                 errorMessage)
            print("Error: ", errorMessage)
    

    我希望它对其他人有用。

    【讨论】:

    • 不正确。 check_output() 将所有输出作为单个字符串返回。 for character in some_string 在 Python 中生成字符,而不是行。此外,当子进程运行时,您的 GUI 将被冻结。您应该使用线程或异步。 io 以避免阻塞 GUI。见code examples in the links
    【解决方案2】:

    带有检查返回码、标准输出和标准错误的完整解决方案:

    import subprocess as sp
    
    # ok
    pipe = sp.Popen( 'ls /bin', shell=True, stdout=sp.PIPE, stderr=sp.PIPE )
    # res = tuple (stdout, stderr)
    res = pipe.communicate()
    print("retcode =", pipe.returncode)
    print("res =", res)
    print("stderr =", res[1])
    for line in res[0].decode(encoding='utf-8').split('\n'):
      print(line)
    
    # with error
    pipe = sp.Popen( 'ls /bing', shell=True, stdout=sp.PIPE, stderr=sp.PIPE )
    res = pipe.communicate()
    print("retcode =", pipe.returncode)
    print("res =", res)
    print("stderr =", res[1])
    

    打印:

    retcode = 0
    res = (b'bash\nbunzip2\nbusybox\nbzcat\n...zmore\nznew\n', b'')
    stderr = b''
    bash
    bunzip2
    busybox
    bzcat
    ...
    zmore
    znew
    
    retcode = 2
    res = (b'', b"ls: cannot access '/bing': No such file or directory\n")
    stderr = b"ls: cannot access '/bing': No such file or directory\n"
    

    【讨论】:

      【解决方案3】:

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

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

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

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

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

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

      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

      【讨论】:

      • 好吧,我实际上将 stdout 和 stderr 输出到一个窗口,然后我需要检查是否有错误并设置一个用于程序另一部分的布尔变量。我尝试了返回码,但无论成功还是错误,它都是无。
      • 好的。还有check_output 方法。您可以从中获取 stdout 和 stderr 并仍然捕获错误。见这里:docs.python.org/2/library/…
      【解决方案4】:

      您可以使用 check_call() 方法检查进程的返回码。 如果进程返回非零值,则会引发 CalledProcessError。

      【讨论】:

      • @Zingam: p.returncode is None 表示进程仍在运行。注意:p.returncodep.communicate() 之后不能是None
      【解决方案5】:
            output,error=pipe.communicate()
      

      这将等待命令完成并根据命令的状态为您提供输出或错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-21
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 2016-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多