1

我有一小段代码(一个较大程序的一部分)只检查驱动器号和 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 的状态更新。任何人都可以验证这一点并提出解决方案吗?

4

0 回答 0