我有一小段代码(大型程序的一部分)只检查驱动器号和CDROM的状态
import wmi
print '--checkCDContent--'
c=wmi.WMI()
cdrom = c.Win32_CDROMDrive()
status = cdrom[0].MediaLoaded
drive = cdrom[0].Drive
print drive, status
作为更大程序的一部分,我必须使用cxFreeze编译代码来创建一个exe。为了测试我的问题,我单独使用上面的代码来复制它。
当我从硬盘驱动器运行.exe时,它正确运行。当我从CD本身运行它(我的最终目标)时,我收到以下错误
X:\>CDrun.exe
--checkCDContent--
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec code in m.__dict__
File "CDrun.py", line 6, in <module>
File "C:\Python27\lib\site-packages\wmi-1.4.9-py2.7-win32.egg\wmi.py", line 1145, in __getattr__
return self._cached_classes (attribute)
File "C:\Python27\lib\site-packages\wmi-1.4.9-py2.7-win32.egg\wmi.py", line 1156, in _cached_classes
self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
File "C:\Python27\lib\site-packages\wmi-1.4.9-py2.7-win32.egg\wmi.py", line 783, in __init__
if namespace:
File "C:\Python27\lib\site-packages\wmi-1.4.9-py2.7-win32.egg\wmi.py", line 1145, in __getattr__
return self._cached_classes (attribute)
File "C:\Python27\lib\site-packages\wmi-1.4.9-py2.7-win32.egg\wmi.py", line 1156, in _cached_classes
self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
File "<COMObject winmgmts:>", line 3, in Get
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 272, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'SWbemServicesEx', u'Not found ', None, 0, -2147217406), None)
我怀疑是它有这个错误,因为代码实际上是从CD运行,同时它试图获得CD的状态更新。任何人都可以验证这一点并建议解决方案吗?