Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Name/ID of removable Media: how?

2 views
Skip to first unread message

Heiko Selber

unread,
Apr 19, 2005, 6:12:06 AM4/19/05
to
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


Tim Golden

unread,
Apr 19, 2005, 6:27:44 AM4/19/05
to pytho...@python.org
[Heiko Selber]

| 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.

Try this:

<code>
import wmi

c = wmi.WMI ()
for i in c.Win32_CDROMDrive ():
print i.Caption, i.VolumeName, i.VolumeSerialNumber

</code>

I haven't answered all your questions, but I'm willing
to bet that you can do pretty much what you want with
WMI.

There are also the pywin32 functions which give you some
of this (maybe all, don't know; haven't tried).

More info here: http://timgolden.me.uk/python/wmi.html
and here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_cdromdrive.asp
and here: http://pywin32.sf.net

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Claudio Grondi

unread,
Apr 19, 2005, 8:46:56 AM4/19/05
to
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" <heiko....@siemens.com> schrieb im Newsbeitrag
news:d42lho$fbf$1...@news.mch.sbs.de...

Ivan Van Laningham

unread,
Apr 19, 2005, 9:40:41 AM4/19/05
to Python Mailing List
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():
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

Heiko Selber

unread,
Apr 19, 2005, 9:44:14 AM4/19/05
to
Aah, nice. Thank you.

This should be included in pywin32, don't you think so? (Or is it? I didn't
check before installing.)

Heiko

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message
news:mailman.2088.1113906...@python.org...

Ivan Van Laningham

unread,
Apr 19, 2005, 11:31:27 AM4/19/05
to Python Mailing List
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?

Tim Golden

unread,
Apr 19, 2005, 11:34:51 AM4/19/05
to Python Mailing List
[Ivan Van Laningham]

| Hi All--
| Tim's wmi stuff looked interesting, so I tried it out, and
| now I have a
| question.
|

[... snip code ...]

| 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?


Try this: hex (-793919769)

You might need to check back on recent discussions here re
negative / positive numbers and hexadecimal.

(Short version: we used to treat hex numbers with the top bit
set as negative decimal numbers; now only negative hex numbers
are negative decimals)

Ivan Van Laningham

unread,
Apr 19, 2005, 11:45:48 AM4/19/05
to Python Mailing List
Hi All--

Tim Golden wrote:
>
> Try this: hex (-793919769)
>
> You might need to check back on recent discussions here re
> negative / positive numbers and hexadecimal.
>
> (Short version: we used to treat hex numbers with the top bit
> set as negative decimal numbers; now only negative hex numbers
> are negative decimals)
>


Of course I tried that. Did you?


Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hex (-793919769)
'-0x2f524119'

Tim Golden

unread,
Apr 19, 2005, 11:49:04 AM4/19/05
to Python Mailing List
[Ivan Van Laningham]

| Hi All--
|
| Tim Golden wrote:
| >
| > Try this: hex (-793919769)
| >
| > You might need to check back on recent discussions here re
| > negative / positive numbers and hexadecimal.
| >
| > (Short version: we used to treat hex numbers with the top bit
| > set as negative decimal numbers; now only negative hex numbers
| > are negative decimals)
| >
|
|
| Of course I tried that. Did you?
|
|
| Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
| win32
| Type "help", "copyright", "credits" or "license" for more information.
| >>> hex (-793919769)
| '-0x2f524119'
| >>>
|

Yes I did. I just forgot to mention the critical fact that I'm still using 2.3.5:

<dump>

Python 2.3.5c1 (#61, Jan 25 2005, 19:52:06) [MSC v.1200 32 bit (Intel)] on win32


Type "help", "copyright", "credits" or "license" for more information.
>>> hex (-793919769)

__main__:1: FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4 and up
'0xd0adbee7'
>>>
>>>

</dump>

Claudio Grondi

unread,
Apr 19, 2005, 5:17:08 PM4/19/05
to
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" <iva...@pauahtun.org> schrieb im Newsbeitrag
news:mailman.2109.1113924...@python.org...

0 new messages