Hey,

Just a simple question really, i was looking through the documentation for python and i couldn't find anything that did basic cd drive things, like ejecting it and stuff. The one module i did find 'cd' was only compatible on IRIX systems.

After failing miserably on google i was just wondering if anyone happened to know of a command or module that opens the cd drive, and reads from it, seems simple but meh..

I'm hoping theres something built in that does it.

Thanks,
a1eio

Dani AI

Generated

There is no cross-platform tray control in the Python stdlib. On Windows, avoid MCI strings and use the storage IOCTLs directly; on Linux/macOS, call the platform tools. Also note: some notebook drives cannot be programmatically closed, and permission/Power Policy can block media load/eject.

# Windows: eject and load an optical drive using pywin32
# Requires: pip install pywin32
import win32file, win32con

DRIVE = r"\\.\E:"  # change to your optical drive letter

h = win32file.CreateFile(
    DRIVE,
    win32con.GENERIC_READ,
    win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
    None,
    win32con.OPEN_EXISTING,
    0,
    None,
)

try:
    # Eject
    win32file.DeviceIoControl(h, 0x2D4808, None, 0, None)  # IOCTL_STORAGE_EJECT_MEDIA
    # ... later, attempt to load (not all drives support this)
    win32file.DeviceIoControl(h, 0x2D480C, None, 0, None)  # IOCTL_STORAGE_LOAD_MEDIA
finally:
    h.Close()

Cross-platform tips:

  • Linux: use subprocess.run(["eject", "/dev/sr0"]) and subprocess.run(["eject", "-t", "/dev/sr0"]). See the eject(1) man page at https://man7.org/linux/man-pages/man1/eject.1.html.
  • macOS: subprocess.run(["drutil", "tray", "open"]) / ... "close". See man drutil.

For status/size/type on Windows, query IOCTL_STORAGE_CHECK_VERIFY and IOCTL_STORAGE_GET_MEDIA_TYPES_EX, or use IMAPI2 COM (MsftDiscMaster2, MediaHeuristicallyBlank) via win32com. Docs: , , . On Linux, mount the disc and use os.statvfs for capacity and blkid/filesystem checks; for blank media or drive status, ioctl via CDROM_DRIVE_STATUS/CDROM_DISC_STATUS from linux/cdrom.h.

Recommended Answers

All 8 Replies

You could use os.popen if you are on unix like operating systems to run shell commands like nautilus-cd-burner or anything you like.

yea, i forgot to mention, i'm trying to do this on a windows machine.

the os.popen idea did occur to me, but then i got stuck again.. :p i don't know the dos command for opening the cd drive.

thanks
a1eio

yea, i forgot to mention, i'm trying to do this on a windows machine.

the os.popen idea did occur to me, but then i got stuck again.. :p i don't know the dos command for opening the cd drive.

thanks
a1eio

if you have pygame, you can use its inbuilt cdrom commands

import pygame.cdrom as cdrom
cdrom.init()
cd = cdrom.CD(0) # 0 = first cdrom device
cd.init()
cd.eject()
cd.quit()
cdrom.quit()

or here's something i found . you can try it out.

Is there any way (non platform-depended) to close the tray using python 3.

I couldn't find any function in the api of pygame.cdrom module. The best result so far is for Windows.

http://code.activestate.com/recipes/180919/

on windows use os.system("YOUR EJECT COMMAND HERE")

Here is one that works on Windows machines with Python3 ...

# open and close the CD tray door (Windows OS)
# tested with Python 3.1

import ctypes
import time

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

# wait 3.5 seconds
time.sleep(3.5)

# close the CD tray (manual closing only with some notebooks)
ctypes.windll.winmm.mciSendStringW("set cdaudio door closed",
    None, 0, None)

Thanks

how can i get status of cd/DVD drive get size of cd, get type(CD/DCD) and know if it blank or..?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.