By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,519 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,519 IT Pros & Developers. It's quick & easy.

Name/ID of removable Media: how?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.FileSystemObje ct") # 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" <he**********@siemens.com> schrieb im Newsbeitrag
news:d4**********@news.mch.sbs.de...
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

Jul 19 '05 #2

P: n/a
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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #3

P: n/a
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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #4

P: n/a
Maybe the function below can help?
I suppose WMI returns same values as FSO (File System Object):

def strDriveHexSerialNumberFromFSOintRetVal(intDriveSe rialNumberByFSO):
"""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(intDriveSe rialNumberByFSO)

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

Claudio

"Ivan Van Laningham" <iv*****@pauahtun.org> schrieb im Newsbeitrag
news:ma**************************************@pyth on.org...
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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.