gpt4 book ai didi

python - shell=True 时如何确定 subprocess.Popen() 失败

转载 作者:太空狗 更新时间:2023-10-29 21:17:04 25 4
gpt4 key购买 nike

Windows 版本的 Python 2.6.4:有什么方法可以确定在使用 shell=True 时 subprocess.Popen() 是否失败?

当 shell=False 时,Popen() 成功失败

>>> import subprocess
>>> p = subprocess.Popen( 'Nonsense.application', shell=False )
Traceback (most recent call last):
File ">>> pyshell#258", line 1, in <module>
p = subprocess.Popen( 'Nonsense.application' )
File "C:\Python26\lib\subprocess.py", line 621, in __init__
errread, errwrite)
File "C:\Python26\lib\subprocess.py", line 830, in
_execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

但是当 shell=True 时,似乎无法确定 Popen() 调用是否成功。

>>> p = subprocess.Popen( 'Nonsense.application', shell=True )
>>> p
>>> subprocess.Popen object at 0x0275FF90>>>
>>> p.pid
6620
>>> p.returncode
>>>

赞赏的想法。

问候,马尔科姆

最佳答案

returncode 将起作用,尽管在您调用 p.poll() 之前它将是 Nonepoll()本身会返回错误代码,所以你可以这样做

if a.poll() != 0:
print ":("

关于python - shell=True 时如何确定 subprocess.Popen() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com