Python 2.4 subproces.CalledProcessError 替换
Python 2.4 subproces.CalledProcessError substitute
我正在尝试向我的子流程添加 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 版本上执行代码,代码将失败,因为 CalledProcessError
是在 Python 版本 2.5 中引入的。
有人知道我可以使用 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
exception subprocess.CalledProcessError
Exception raised when a process run by check_call() or check_output() returns a non-zero exit status.
returncode
Exit status of the child process.
cmd
Command that was used to spawn the child process.
output
Output of the child process if this exception is raised by check_output(). Otherwise, None.
Source。这意味着您需要检查 check_all 或 check_output 的过程 运行 是否具有非零输出。
我正在尝试向我的子流程添加 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 版本上执行代码,代码将失败,因为 CalledProcessError
是在 Python 版本 2.5 中引入的。
有人知道我可以使用 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
exception subprocess.CalledProcessError
Exception raised when a process run by check_call() or check_output() returns a non-zero exit status. returncode Exit status of the child process. cmd Command that was used to spawn the child process. output Output of the child process if this exception is raised by check_output(). Otherwise, None.
Source。这意味着您需要检查 check_all 或 check_output 的过程 运行 是否具有非零输出。