473,395 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Name/ID of removable Media: how?

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
4 2885
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Garet Cammer | last post by:
Hi Everyone, I am writing a program to perform backup operations using the VB scripting File System Object to copy to removable media storage devices. The backup operation works fine, but when we...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
1
by: Cliff Liao | last post by:
Hi all, I am trying to install the .NET framework 1.1 runtime package on Windows 2000 SP4. While the installation starts well, I get the following message box during the installation process :...
0
by: Ryan | last post by:
I'm writing a utility that needs to be aware of media (CD, DVD, Zip disk, usb stick, etc) insertions and removals. Which seems to be more of a problem than I original imagined. I can't use any...
3
by: Max | last post by:
Not sure exactly how to describe this or if it's even possible, so I'll do my best and would appreciate some suggestions. I'm currently working on a service that will allow me to automate some...
0
by: Allen | last post by:
Dim DiskMO As New ManagementObject("win32_logicaldisk.DeviceID=""" & sDrive & """") Then using DiskMO("DriveType") Case DRIVE_TYPES_REMOVABLE snip...
3
by: Mike Joyce | last post by:
I am trying to write a portable script that will find removable media, such as compact flash, sd card, usb, etc. drive and then upload files from the media. I want this to be portable so that I can...
7
by: glenn | last post by:
Hi can anyone tell me how given a directory or file path, I can pythonically tell if that item is on 'removable media', or sometype of vfs, the label of the media (or volume) and perhaps any...
4
by: Per Juul Larsen | last post by:
Hi my VB program must always start from a removable disk. In addition, on the removable media to be images of the type *. jpg *. tips. If not the two conditions are present, the programme will end...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.