Name/ID of removable Media: how?

Discussion in 'Python' started by Heiko Selber, Apr 19, 2005.

  1. Heiko Selber

    Heiko Selber Guest

    I am trying to find out (using Python under windows) the name of a CD that
    is currently in the drive specified by a path name.

    And while I am at it, I'd also like to know whether the specified drive
    contains a CD at all, and whether the drive is actually a CD drive.

    AFAIK, Python doesn't provide a way to do it, and a search in the web
    yielded only soutions for Linux.

    Can anyone show me a solution that works with windoze?


    Heiko Selber, Apr 19, 2005
    1. Advertisements

  2. There are sure thousand ways
    of doing it "with windoze".
    Here one of them (NOT tested) in form
    of code snippets you can rearrange
    for your purpose:

    import win32com.client
    axFSO = win32com.client.Dispatch("Scripting.FileSystemObject") # SCRRUN.DLL
    axLstDrives = axFSO.Drives

    dctAXaxFSO_NumCodeAsKeyVsTypeDescrText = {
    0 : "unknown type of drive"
    , 1 : "drive with removable medium (e.g. Floppy)"
    , 2 : "fixed drive (e.g. harddisk)"
    , 3 : "remote (i.e. network) drive"
    , 4 : "CD-ROM drive"
    , 5 : "RAM-Disk drive"

    lstdriveLetterOFonSYSTstorageDevice = []
    lstdriveTypeOFonSYSTstorageDevice = []

    for axDrive in axLstDrives:
    if(axDrive.IsReady): # checks if a CD is inserted
    # axDrive.SerialNumber
    # axDrive.VolumeName.encode()
    # for more of this kind just check out e.g. in the by Microsoft
    # free available JScript tutorial the chapter
    # "FileSystemObject Sample Code"

    Hope this helps.

    Claudio Grondi, Apr 19, 2005
    1. Advertisements

  3. Hi All--

    This works. The only thing you have to do is stuff a floppy into the
    drive & find out what the fstype is (that's inf[-1] in the code below)
    so you can key on it. Try the docs for Mark Hammond's Win32, and
    there's always his _Python Programming on Win32_.

    import os
    import os.path
    import win32api

    def findCDs():
    print "Searching for cd drives..."
    for i in drives:
    if dr.isalpha():
    pass # Removable drive, not ready
    if inf!=None:
    if inf[-1].lower().endswith("cdfs"):
    elif inf[-1].lower().endswith("udf"):
    return cdDrives

    inf[0] contains the volume label if there is one and if there's a CD

    Note that you must use the full drive spec, "letter:\\" or
    GetVolumeInformation() doesn't always work.

    There are probably better ways to do these things, but they do work;
    I've been using them constantly the last few days.

    Ivan Van Laningham
    God N Locomotive Works
    Army Signal Corps: Cu Chi, Class of '70
    Author: Teach Yourself Python in 24 Hours
    Ivan Van Laningham, Apr 19, 2005
  4. Hi All--
    Tim's wmi stuff looked interesting, so I tried it out, and now I have a


    import wmi
    import win32api

    for i in c.Win32_CDROMDrive():
    print "WMI serial",v,long(v,0x10)

    print "win32api serial",sn,long(sn)

    The output from the above script (drive d contains cd) is:

    WMI serial D0ADBEE7 3501047527

    win32api serial -793919769 -793919769

    What's the difference between the two serial numbers? WMI is returning
    a long converted to a hex repr string, while win32api is returning an
    int (type(sn) is <type 'int'>), & converting to hex bears no resemblance
    to what WMI shows. What am I missing?

    Ivan Van Laningham
    God N Locomotive Works
    Army Signal Corps: Cu Chi, Class of '70
    Author: Teach Yourself Python in 24 Hours
    Ivan Van Laningham, Apr 19, 2005
  5. Maybe the function below can help?
    I suppose WMI returns same values as FSO (File System Object):

    def strDriveHexSerialNumberFromFSOintRetVal(intDriveSerialNumberByFSO):
    """Supplied with an integer representing a drive serial number returns the
    number in same format as \>dir command does (i.e. ####-####)."""
    # The value of axDrive.SerialNumber property (an int or float number) can
    # negative, so to get always a positive number as listed by e.g. \>dir
    # command it is necessary to apply a workaround:
    if(intDriveSerialNumberByFSO < 0):
    # dec 4294967295 == hex FFFFFFFF, dec 4294967296 == hex 100000000
    intDriveSerialNumber = 4294967296L + intDriveSerialNumberByFSO
    intDriveSerialNumber = intDriveSerialNumberByFSO
    strHexSerialNumber = "%08X"%intDriveSerialNumber
    strHexSerialNumber = strHexSerialNumber[:4]+"-"+strHexSerialNumber[4:]
    return strHexSerialNumber
    #:def strDriveHexSerialNumberFromFSOintRetVal(intDriveSerialNumberByFSO)

    In your case

    Claudio Grondi, Apr 19, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.