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?

    TIA,

    Heiko
    Heiko Selber, Apr 19, 2005
    #1
    1. Advertising

  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
    lstdriveLetterOFonSYSTstorageDevice.append(
    axDrive.DriveLetter.encode()
    )
    lstdriveTypeOFonSYSTstorageDevice.append(
    dctAXaxFSO_NumCodeAsKeyVsTypeDescrText[axDrive.DriveType]
    )
    # 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"
    #:if
    #:for

    Hope this helps.

    Claudio



    "Heiko Selber" <> schrieb im Newsbeitrag
    news:d42lho$fbf$...
    > 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?
    >
    > TIA,
    >
    > Heiko
    >
    >
    Claudio Grondi, Apr 19, 2005
    #2
    1. Advertising

  3. 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
    Ivan Van Laningham, Apr 19, 2005
    #3
  4. Hi All--
    Tim's wmi stuff looked interesting, so I tried it out, and now I have a
    question.


    -----
    #!/usr/bin/python

    import wmi
    import win32api


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

    vn,sn,ln,flg,fstype=win32api.GetVolumeInformation("d:\\")
    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?

    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
    Ivan Van Laningham, Apr 19, 2005
    #4
  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
    be
    # 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
    else:
    intDriveSerialNumber = intDriveSerialNumberByFSO
    #:if/else
    strHexSerialNumber = "%08X"%intDriveSerialNumber
    strHexSerialNumber = strHexSerialNumber[:4]+"-"+strHexSerialNumber[4:]
    return strHexSerialNumber
    #:def strDriveHexSerialNumberFromFSOintRetVal(intDriveSerialNumberByFSO)

    In your case
    strDriveHexSerialNumberFromFSOintRetVal(-793919769):
    gives:
    D0AD-BEE7

    Claudio

    "Ivan Van Laningham" <> schrieb im Newsbeitrag
    news:...
    > Hi All--
    > Tim's wmi stuff looked interesting, so I tried it out, and now I have a
    > question.
    >
    >
    > -----
    > #!/usr/bin/python
    >
    > import wmi
    > import win32api
    >
    >
    > c=wmi.WMI()
    > for i in c.Win32_CDROMDrive():
    > v=i.VolumeSerialNumber
    > print "WMI serial",v,long(v,0x10)
    >
    > vn,sn,ln,flg,fstype=win32api.GetVolumeInformation("d:\\")
    > 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?
    >
    > 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
    Claudio Grondi, Apr 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Goulet

    Removable Drive

    David Goulet, Jan 20, 2004, in forum: Java
    Replies:
    2
    Views:
    378
    Christophe Vanfleteren
    Jan 20, 2004
  2. Tim Golden

    RE: Name/ID of removable Media: how?

    Tim Golden, Apr 19, 2005, in forum: Python
    Replies:
    1
    Views:
    345
    Heiko Selber
    Apr 19, 2005
  3. Tim Golden

    RE: Name/ID of removable Media: how?

    Tim Golden, Apr 19, 2005, in forum: Python
    Replies:
    0
    Views:
    355
    Tim Golden
    Apr 19, 2005
  4. Mike Joyce
    Replies:
    3
    Views:
    3,119
    Gordon Rainsford
    May 3, 2006
  5. glenn

    analyzing removable media

    glenn, Sep 29, 2006, in forum: Python
    Replies:
    8
    Views:
    334
    glenn
    Sep 30, 2006
Loading...

Share This Page