473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help controlling CDROM from python

Hello,

I am trying to control a CD-ROM drive using python. The code I use is
shown below.
import CDROM
from fcntl import ioctl
import os
class Device:

CDdevice=""
CDfd = None

def __init__(self,name):
self.CDdevice = name #we get a device name when module loaded

if self.CDdevice == "":
print "No device specified"
sys.exit(-1)
self.openCD()

def openCD(self):

try:
self.CDfd = open(self.CDdevice, 'r') #open the device and return filedescriptor

except(OSError,IOError): #if there is an OS or IO Error (usually indicates nodisk)
print "Device Error, Halting.... (usually means drive or disk not found)"
sys.exit(-1)
> def unlockCD(self):
return self.sendCDcommand(CDROM.CDROM_LOCKDOOR,0)
def ejectCD(self):
self.unlockCD() #we need to unlock the CD tray before we try to eject, otherwise we get an IO Error (#5)
return self.sendCDcommand(CDROM.CDROMEJECT)
def sendCDcommand(self,command,argument=''):
return ioctl(self.CDfd,command,argument)


The code that calls the class is a follows:
import CD_Bindings

CD = CD_Bindings.Device("/dev/cdrom")

print CD.ejectCD()
This works great, but only when there is a disk inside, otherwise we get
an error.

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.

Thanks,

Ognjen
Mar 10 '07 #1
3 8989
On Mar 10, 8:27 am, Ognjen Bezanov <Ogn...@mailshack.comwrote:
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/pyt...er/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.hon 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

Mar 10 '07 #2
On Mar 10, 4:11 pm, "MonkeeSage" <MonkeeS...@gmail.comwrote:
win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ,
FILE_SHARE_READ,
None, OPEN_EXISTING, 0, 0)
Oops! That should have been:

h = win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ,
FILE_SHARE_READ, None,
OPEN_EXISTING, 0, 0)

Mar 11 '07 #3
If you want an OS neutral way one may be able to use pycdio from the
Cheese shop.

It requires libcdio to be installed and that sometimes the case if you
have a free media player (like vlc or xine, or mplayer) installed.

I don't really use it all that often so I can't vouch for how good it
is. (Although there *are* regression tests).

Ognjen Bezanov <Og****@mailshack.comwrites:
Hello,

I am trying to control a CD-ROM drive using python. The code I use is
shown below.
import CDROM
from fcntl import ioctl
import os
class Device:

CDdevice=""
CDfd = None

def __init__(self,name):
self.CDdevice = name #we get a device name when module loaded

if self.CDdevice == "":
print "No device specified"
sys.exit(-1)
self.openCD()

def openCD(self):

try:
self.CDfd = open(self.CDdevice, 'r') #open the device and return filedescriptor

except(OSError,IOError): #if there is an OS or IO Error (usually indicates nodisk)
print "Device Error, Halting.... (usually means drive or disk not found)"
sys.exit(-1)
def unlockCD(self):
return self.sendCDcommand(CDROM.CDROM_LOCKDOOR,0)
def ejectCD(self):
self.unlockCD() #we need to unlock the CD tray before we try to eject, otherwise we get an IO Error (#5)
return self.sendCDcommand(CDROM.CDROMEJECT)

def sendCDcommand(self,command,argument=''):
return ioctl(self.CDfd,command,argument)

The code that calls the class is a follows:
import CD_Bindings

CD = CD_Bindings.Device("/dev/cdrom")

print CD.ejectCD()

This works great, but only when there is a disk inside, otherwise we get
an error.

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.

Thanks,

Ognjen
Mar 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: enclume42 | last post by:
Hello, I am a pure Linux guy. I wrote a Java program that is going to be used by some Windows people, and I wish the startup of the program to be as simple as possible, namely to double-click on...
0
by: martin z | last post by:
I'm having a lot of trouble figuring out the details of controlling Python compiling. I can compile and embed just fine, but I want some finer granularity on what gets compiled into Python. I can...
0
by: Bart Simpson | last post by:
Hi, I would like to create/delete ftp accounts of windows IIS, and to control MS Media Service under windows 2003 by Python program. A few days ago, I found there is iisftp.vbs vb-script that...
4
by: PGMoscatt | last post by:
Hi all, Have just installed Fedora 3 and wish also want to give programming with Python a go. I wish to develop GUI interfaces and believe that 'Tkinter' is the way to go. After installing...
4
by: anton | last post by:
Hi, I am googeling some hours now ... still without result. So I have a question: Does somebody know a filemanager: - which looks like Norton Commander/7-Zip Filemanager
5
by: Maria João | last post by:
I have a table with one field that ia a hyperlink to a pdf file that opens a specific file. Since these files are very big, I would need to change the hyperlink path, so that I could send the...
3
by: Fabian Braennstroem | last post by:
Hi, I look for an easy way to use the newest scipy, pyvtk, matplotlib, f2py, numpy, paraview/vtk,... on a entreprise redhat machine without administration rights. My first thought was to...
6
by: AOECheatmaster | last post by:
I am trying to write a C++ autorun launcher, but i have a problem, i cannot figure out how to set the buttons to launch the apps to use whatever drive number the CDROM is located at, all i can...
3
by: Klapet | last post by:
Hi, I'm quite new in C++ programing and I want to ask, if I can handle events in simple console-like program. For example I need one loop to repeat until some key is pressed. Is it possible to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.