Name/ID of removable Media: how?

Ivan Van Laningham ivanlan at pauahtun.org
Tue Apr 19 09:40:41 EDT 2005


Hi All--

Heiko Selber wrote:
> 
> 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?
> 

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():
    cdDrives=[]
    print "Searching for cd drives..."
    drives=win32api.GetLogicalDriveStrings().split(":")
    for i in drives:
        dr=i[-1].lower()
        if dr.isalpha():
            dr+=":\\"
            inf=None
            try:
                inf=win32api.GetVolumeInformation(dr)
            except:
                pass # Removable drive, not ready
            if inf!=None:
                if inf[-1].lower().endswith("cdfs"):
                    cdDrives.append([dr,inf])
                elif inf[-1].lower().endswith("udf"):
                    cdDrives.append([dr,inf])
    return cdDrives

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

>>> win32api.GetVolumeInformation("c:\\")
('God_C', -798323922, 255, 459007, 'NTFS')
>>>

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.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours



More information about the Python-list mailing list