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
会起作用,尽管它会None
直到你打来电话p.poll()
. poll()本身会返回错误代码,所以你可以这样做
if a.poll() != 0:
print ":("
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)