How to determine subprocess.Popen() failed when shell=True

后端 未结 2 400
心在旅途
心在旅途 2021-01-18 01:40

Windows version of Python 2.6.4: Is there any way to determine if subprocess.Popen() fails when using shell=True?

Popen() successfully fails when shell=False

相关标签:
2条回答
  • 2021-01-18 01:50

    returncode will work, although it will be None until you've called p.poll(). poll() itself will return the error code, so you can just do

    if a.poll() != 0:
        print ":("
    
    0 讨论(0)
  • 2021-01-18 01:57

    In the first case it fails to start, in the second - it successfully starts shell which, in turn, fails to execute the application. So your process has been properly spawned, exited and waits for you to inquire about its exit code. So, the thing is, unless your shell or environment (e.g. no memory) is utterly broken there's no way Popen itself may fail.

    So, you can safely .poll() and .wait() on it to get all the sad news.

    0 讨论(0)
提交回复
热议问题