How to eject CD using WMI and Python?

前端 未结 2 1594
南旧
南旧 2021-02-08 22:40

Using Windows\' WMI library, how can I eject CD rom mounted in a specific CD/DVD drive?

I am asking for sources from WMI docs or examples since I am using wmi.py library

2条回答
  •  自闭症患者
    2021-02-08 23:30

    You can use ctypes.

    import ctypes
    
    ctypes.windll.WINMM.mciSendStringW(u"set cdaudio door open", None, 0, None)
    

    UPDATE:

    If you have more that one drive, you can use to open command to initialize a specific device before calling the function above. For example (not tested).

    ctypes.windll.WINMM.mciSendStringW(u"open D: type cdaudio alias d_drive", None, 0, None)
    ctypes.windll.WINMM.mciSendStringW(u"set d_drive door open", None, 0, None)
    

    Also, see the documentation on how to check return values

提交回复
热议问题