【发布时间】:2015-04-29 01:43:12
【问题描述】:
我正在尝试将try/except 添加到我的子进程中。
try:
mountCmd = 'mount /dev/%s %s%s' % (splitDevice, homeDir, splitDevice)
dev = '/dev/%s' % splitDevice
subprocess.check_call(mountCmd, shell=True)
except subprocess.CalledProcessError:
continue
上面的代码片段有效,但如果主机在低于 2.5 的 Python 版本上执行代码,则代码将失败,因为在 Python 2.5 版本中引入了CalledProcessError。
有人知道我可以为CalledProcessError 模块使用的替代品吗?
编辑: 这就是我解决问题的方法
mountCmd = 'mount /dev/%s %s%s' % (splitDevice, homeDir, splitDevice)
dev = '/dev/%s' % splitDevice
returnCode = 0
#CalledProcessError module was introduced in version 2.5 of python. If older version do the following.
if sys.hexversion < 0x02050000:
try:
p3 = subprocess.Popen(mountCmd, shell=True, stdout=subprocess.PIPE)
output = p3.communicate()[0]
returnCode = p3.returncode
except:
pass
if returnCode != 0:
continue
else: #If version of python is newer than 2.5 use CalledProcessError.
try:
subprocess.check_call(mountCmd, shell=True)
except subprocess.CalledProcessError, e:
continue
【问题讨论】:
标签: python python-2.4 python-2.3