python How to detect a new piece of media in the CD?

回眸只為那壹抹淺笑 提交于 2019-12-11 16:55:31

问题


I have a need to copy a group of files. Unfortunately these files will span multiple DVDs. What I want to do is

a) copy the files of the current DVD
b) when complete, eject the media and prompt the user to insert the next DVD
c) Detect when media is inserted
d) validate that is is the desired DVD (if not do B again)
e) copy files
f) repeat as needed

Im fairly sure I know how to do all of this except for step C. How do I detect when a new CD has been inserted?


回答1:


I ended up using a combination of wmi and ctypes

import wmi
import os
import time
import wx
import ctypes

app = wx.PySimpleApp(0)
c = wmi.WMI()

for cdrom in c.Win32_CDROMDrive():
    status = cdrom.MediaLoaded
    drive = cdrom.Drive


checkFile = os.path.join(drive,'IWPCpatch-2','install.zip')
print checkFile

testForFile = os.path.exists(checkFile)
while testForFile == False:
    print 'file present', testForFile

    for cdrom in c.Win32_CDROMDrive():
        status = cdrom.MediaLoaded
        print 'Media present', status

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

    #Warn
    sMessage = """ Please insert the media that contains the file """ + checkFile

    successWarning = wx.MessageBox(sMessage, "WARNING")

    #wait for new cd
    for cdrom in c.Win32_CDROMDrive():
        status = cdrom.MediaLoaded
        print 'Media present', status

    while status == False:
        for cdrom in c.Win32_CDROMDrive():
            status = cdrom.MediaLoaded
            print 'Media present', status
        time.sleep(5)

    #test and exit loop or restart
    testForFile = os.path.exists(checkFile)


print 'FILE PASSED'



回答2:


pygame has a module for managing the CD/DVD drive: http://www.pygame.org/docs/ref/cdrom.html




回答3:


PyMedia looks like it has what you need.



来源:https://stackoverflow.com/questions/6376596/python-how-to-detect-a-new-piece-of-media-in-the-cd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!