Python 如何检测CD中的新媒体?

0 投票
3 回答
2803 浏览
提问于 2025-04-16 19:43

我需要复制一组文件。可惜这些文件会跨越多张DVD。我想做的是

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

我对这些步骤都比较有把握,除了步骤C。我该怎么知道什么时候插入了一张新的光盘呢?

3 个回答

0

PyMedia 这个库看起来正好符合你的需求。

0

pygame有一个模块可以用来管理光盘/DVD驱动器,具体内容可以查看这里:http://www.pygame.org/docs/ref/cdrom.html

1

我最后使用了wmi和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'

撰写回答