Python Programming Language
Help controlling CDROM from python
I am trying to control a CD-ROM drive using python. The code I use is
The code that calls the class is a follows:
This works great, but only when there is a disk inside, otherwise we get
> import CD_Bindings
> CD = CD_Bindings.Device("/dev/cdrom")
> print CD.ejectCD()
My issue is that I need to be able to eject the CDROM tray even if there
is no disk inside.
This is possible because other programs (like the linux "eject" command)
can do it. Its just a question of how it is done in python. So I'm
posting here in the hope someone can tell me.
On Mar 10, 8:27 am, Ognjen Bezanov <Ogn@mailshack.com> wrote:
> My issue is that I need to be able to eject the CDROM tray even if there Here's a Q&D version (haven't tested the windows part, it's from an
> is no disk inside.
old mailing list post, but it looks correct):
import os, sys
if 'win' in sys.platform:
# untested: found in old mailing list post:
from win32con import *
drive = 'D:'
win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ,
None, OPEN_EXISTING, 0, 0)
# IOCTL_STORAGE_EJECT_MEDIA = 0x002d4808 from <winioctl.h>
win32file.DeviceIoControl(h, 0x002d4808, "", 0)
else: # does this work on OSX and BSD?
cd_device = '/dev/cdrom'
base_path = os.path.dirname(cd_device)
cd_device = os.readlink(cd_device)
if not cd_device == '/':
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)
NB: If you're using pygame, you can also just use it's builtin
import pygame.cdrom as cdrom
cd = cdrom.CD(0) # 0 = first cdrom device
On Mar 10, 4:11 pm, "MonkeeSage" <MonkeeS@gmail.com> wrote:
> win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ, Oops! That should have been:
> None, OPEN_EXISTING, 0, 0)
h = win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ,
OPEN_EXISTING, 0, 0)