Help controlling CDROM from python
MonkeeSage
MonkeeSage at gmail.com
Sat Mar 10 17:11:59 EST 2007
On Mar 10, 8:27 am, Ognjen Bezanov <Ogn... at mailshack.com> wrote:
> My issue is that I need to be able to eject the CDROM tray even if there
> is no disk inside.
Here's a Q&D version (haven't tested the windows part, it's from an
old mailing list post, but it looks correct):
import os, sys
if 'win' in sys.platform:
# untested: found in old mailing list post:
# http://mail.python.org/pipermail/python-win32/2002-November/000593.html
import win32file
from win32con import *
drive = 'D:'
win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ,
FILE_SHARE_READ,
None, OPEN_EXISTING, 0, 0)
# IOCTL_STORAGE_EJECT_MEDIA = 0x002d4808 from <winioctl.h>
win32file.DeviceIoControl(h, 0x002d4808, "", 0)
win32file.CloseHandle(h)
else: # does this work on OSX and BSD?
import fcntl
cd_device = '/dev/cdrom'
if os.path.islink(cd_device):
base_path = os.path.dirname(cd_device)
cd_device = os.readlink(cd_device)
if not cd_device[0] == '/':
cd_device = os.path.join(base_path, cd_device)
cdrom = os.open(cd_device, os.O_RDONLY | os.O_NONBLOCK)
# CDROMEJECT = 0x5309 from <linux/cdrom.h> on linux 2.6
fcntl.ioctl(cdrom, 0x5309, 0)
os.close(cdrom)
NB: If you're using pygame, you can also just use it's builtin
commands:
import pygame.cdrom as cdrom
cdrom.init()
cd = cdrom.CD(0) # 0 = first cdrom device
cd.init()
cd.eject()
cd.quit()
cdrom.quit()
Regards,
Jordan
More information about the Python-list
mailing list