python How to detect a new piece of media in the C

2019-09-13 11:18发布

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?

标签: python media
3条回答
仙女界的扛把子
2楼-- · 2019-09-13 11:40

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'
查看更多
地球回转人心会变
3楼-- · 2019-09-13 11:47

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

查看更多
迷人小祖宗
4楼-- · 2019-09-13 12:03

PyMedia looks like it has what you need.

查看更多
登录 后发表回答