我有以下功能:
def findHardDriveLetter(drivename):
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
for drive in drives:
try:
volname = win32api.GetVolumeInformation(drive)[0].upper()
except:
pass
if volname == drivename.upper():
return drive
根据驱动器状态,可能会发生此错误,我希望除了捕获特定错误之外的其他错误:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<editor selection>", line 5, in findHardDriveLetter
error: (21, 'GetVolumeInformation', 'The device is not ready.')
使用type(exception).__name__
,错误将重新发布为类型错误。这似乎与Python错误类型的典型格式不同,如果我使用
except error:
要抓住它,我得到了这个例外:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<editor selection>", line 20, in findHardDriveLetter
NameError: global name 'error' is not defined
那么为什么这不能像我期望的那样工作,如何在没有通用的情况下捕获此异常,除了?
答案 0 :(得分:1)
你可以除了ArrayList<String> res = new ArrayList<String>();
,因为这是你得到的异常类型,但它通常被用作所有win32api异常的基类......很奇怪
win32api.error