satoru
satoru

Reputation: 33225

How can I eject a CD from MacOS with Python?

I'm trying to use something like ioctl(fd, CDROMEJECT, 0), but I don't know how to get the fd referring to the CDROM in the first place.

I've looked into /dev and /Volumes and found nothing related.

Is there any other way to execute the ejection?

Upvotes: 2

Views: 2084

Answers (1)

Jared Burrows
Jared Burrows

Reputation: 55535

Have you tried using a system call?

Mac OS X

Eg. Try this in your Terminal

python -c 'import os; os.system("drutil tray open")'

I just tried this on my MacBook Pro, let me know if this helps!

Code:

import os

os.system("drutil tray open")

Source: http://osxdaily.com/2007/03/26/quick-tip-eject-media-from-the-command-line/

Linux

import os

os.system("eject -t cdrom")

Source: http://opentechlab.blogspot.com/2009/06/play-with-cdrom-in-linux.html

Windows

Using "cytpes" module:

import ctypes, time

# open the CD tray 
ctypes.windll.winmm.mciSendStringW("set cdaudio door open", None, 0, None)

# try closing the tray
ctypes.windll.winmm.mciSendStringW("set cdaudio door closed", None, 0, None)

Source: http://www.daniweb.com/software-development/python/threads/72834/handling-cd-drives-from-python

Universal

Using "pygame" module: Download here

import pygame.cdrom as cdrom

cdrom.init()
cd = cdrom.CD(0)
cd.init()
cd.eject()
cd.quit()
cdrom.quit()

Source: http://bytes.com/topic/python/answers/614751-help-controlling-cdrom-python

Using "pymedia" module: Download here:

Documentation: http://www.pygame.org/docs/ref/cdrom.html

http://pymedia.org/docs/pymedia.removable.cd.html

There seems to be an 'eject' function within the library:

eject() eject or open the cdrom drive eject() -> None This will open the cdrom drive and eject the cdrom. If the drive is playing or paused it will be stopped.

Source: http://www.pygame.org/docs/ref/cdrom.html#pygame.cdrom.CD.eject

Regards,

Upvotes: 5

Related Questions