Python Forum
How to get a removable disc type in drive - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to get a removable disc type in drive (/thread-41560.html)

Pages: 1 2


RE: How to get a removable disc type in drive - Daring_T - Feb-09-2024

(Feb-08-2024, 06:35 AM)Gribouillis Wrote:
(Feb-08-2024, 05:56 AM)Daring_T Wrote: Also here is what a blue-ray looks like with running dict(dev.properties):
The property ID_CDROM_MEDIA_BD tells you that there is a blueray disk

Thanks Gribouillis! I don't know how I missed that variable. I was looking for a good bit through my logs. This is the code I ended up using just in case anymore needs it. Thanks again for all your guy's help on this one! I couldn't of figured it out without you all.

import pyudev

def get_drive_type(drive_name="sr0"):
    context = pyudev.Context()
    a = context.list_devices(sys_name=drive_name)
    dev = next(iter(a))
    # print(dev)
    # print(dict(dev.properties))
    
    disc_type = None
    
    if dev.properties.get("ID_CDROM_MEDIA_CD", False):
        disc_type = "CD"        

    elif dev.properties.get("ID_CDROM_MEDIA_DVD", False):
        disc_type = "DVD"        

    elif dev.properties.get("ID_CDROM_MEDIA_BD", False):
        disc_type = "BLUERAY"

    print(f"Drive contains a: {disc_type} in Drive: {drive_name}")
    return disc_type
 
if __name__ == "__main__":
    get_drive_type()



RE: How to get a removable disc type in drive - Daring_T - Feb-11-2024

(Feb-08-2024, 06:35 AM)Gribouillis Wrote:
(Feb-08-2024, 05:56 AM)Daring_T Wrote: Also here is what a blue-ray looks like with running dict(dev.properties):
The property ID_CDROM_MEDIA_BD tells you that there is a blueray disk

If anyone needs it, I have also added support for CD-ROMS as shown below.

def get_drive_type(drive_name="sr0"):
    '''Returns the type of Disc in Drive'''

    context = pyudev.Context()
    a = context.list_devices(sys_name=drive_name)
    dev = next(iter(a))

    disc_type = None

    if dev.properties.get("ID_CDROM_MEDIA_CD", False):
        if dev.properties.get('ID_FS_USAGE', False):
            disc_type = "CDROM"
        else:
            disc_type = "CDAUDIO"

    elif dev.properties.get("ID_CDROM_MEDIA_DVD", False):
        disc_type = "DVD"

    elif dev.properties.get("ID_CDROM_MEDIA_BD", False):
        disc_type = "BLUERAY"
    
    return disc_type


if __name__ == "__main__":
    disc_type = get_drive_type(drive_name="sr0")

    print(f"Drive: {drive_name} contains a: {disc_type}.")



RE: How to get a removable disc type in drive - Gribouillis - Feb-11-2024

(Feb-11-2024, 07:39 AM)Daring_T Wrote: I have also added support for CD-ROMS as shown below.
You don't need the False parameter in get() because get() normally returns None when the key is not in the mapping. Note that the pyudev.device._device.Properties inherits collections.abc.Mapping which defines the get() method's interface.