[python-win32] Volume Serial Number

Tim Roberts timr at probo.com
Fri May 16 19:33:39 CEST 2008


Rickey, Kyle W wrote:
>
> How do I get the volume serial number for a drive? For example in the 
> cmd prompt issuing this:
>
>  
>
> C:\>vol C:
>
>  Volume in drive C is LABEL
>
>  Volume Serial Number is FFFF-0000
>
>  
>
> Is the volume serial number wrapped by pywin32? I found the function 
> win32api.GetVolumeInformation, but it doesn’t seem to return what I want.
>

Yes, actually, it does.  The second item in the tuple it returns is the 
VSN as a 32-bit binary value.

You were probably confused because Python showed it to you as a signed 
decimal integer.  If your VSN is really FFFF-0000, then it probably 
showed you -65535.  If you print that in hex, you get -FFFF, and if you 
treat that as unsigned, you get FFFF0000.

I wish I knew a better way to do the signed/unsigned mangling, but try this:

    def UnsignedHex( v ):
        if v >= 0: return '%08X' % v
        return '%08X' % (0x100000000 + v )
    print UnsignedHex( win32api.GetVolumeInformation('C:\\')[1] )

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list