问题
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