Python Forum
How to get a removable disc type in drive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get a removable disc type in drive
#11
(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()
Reply
#12
(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}.")
Reply
#13
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  open python files in other drive akbarza 1 705 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Integrating Google Drive in App Lahearle 0 476 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  code to send attachments contained on the drive. stefanoste78 1 868 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,819 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Cant Access D: Drive kucingkembar 4 1,356 Jul-28-2022, 04:53 PM
Last Post: kucingkembar
  Calling exe on network drive GrahamL 4 2,559 Jan-21-2022, 12:01 PM
Last Post: GrahamL
  Failing to copy file from a network to a local drive tester_V 4 7,148 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Type hinting - return type based on parameter micseydel 2 2,504 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Nested Subdirectory within current sub on shared network drive Agregware 1 1,944 Jun-19-2019, 10:07 PM
Last Post: Gribouillis
  trouble with os.listdir on a network drive lconner 10 19,124 Jun-04-2019, 07:16 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020