Determining the volume ID for a file/drive?

Alex Martelli aleax at aleax.it
Tue Apr 29 08:11:21 EDT 2003


carroll at tjc.com wrote:

> Is there any way to determine the volume serial number for a disk,
> given the filename (or drive, since I can determine the drive from the
> filename with os.path.splitdrive(os.path.abspath(file))?
> 
> My environment is Windows & Python 2.2.2, and I don't need for my code
> to be portable.

Quick and dirty...:

import os
x = os.popen('C:\\')
x.readline(); x.readline()
vsn = x.readline().strip()[-9:]
x.close()


Perhaps too dirty.  If you want to do it via a Win32 API call, you
need to determine what call is it (e.g. via MSDN) and then use
win32all or ctypes to go and call it.  But parsing dir's output
is quite a bit quicker to program;-).


Alex






More information about the Python-list mailing list