Get the hard disk hardware serial number

Dietmar Schwertberger news at schwertberger.de
Thu Jun 4 09:15:25 EDT 2009


MRAB schrieb:
> Jorge wrote:
>> I need to know how to get the hardware serial number of a hard disk in 
>> python.
>>
> For Windows, see http://www.daniweb.com/forums/thread187326.html

This recipe uses the function GetVolumeInformation(), which does not
return the hardware serial number.

 From the microsoft documentation:
  This function returns the volume serial number that the operating
  system assigns when a hard disk is formatted. To programmatically
  obtain the hard disk's serial number that the manufacturer assigns,
  use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia
  property SerialNumber.

The WMI method is e.g. described here:
http://www.velocityreviews.com/forums/t359670-wmi-help.html


import wmi
c = wmi.WMI()
for pm in c.Win32_PhysicalMedia():
     print pm.Tag, pm.SerialNumber

or to retrieve the serial number for the installation drive:

serial = c.Win32_PhysicalMedia(["SerialNumber"], 
Tag=r"\\.\PHYSICALDRIVE0")[0].SerialNumber.strip()


Regards,

Dietmar



More information about the Python-list mailing list