This article describes how to use the wmi module in python to obtain hard disk information in windows, and related skills related to how to use Python to obtain system hardware information, for more information about how to use the wmi module in python to obtain hard disk information in windows, see the following example. Share it with you for your reference. The specific implementation method is as follows:
#-*-Coding: UTF-8-*-# import ################################## ###################################### import OS, sys import time import wmi ################################### ###################################### function # ######################################## ############################### def get_disk_info (): "to obtain physical disk information. "Tmplist = [] c = wmi. WMI () for physical_disk in c. win32_DiskDrive (): tmpdict ={} tmpdict ["Caption"] = physical_disk.Caption tmpdict ["Size"] = long (physical_disk.Size)/1024/1024/1024 tmplist. append (tmpdict) return tmplist def get_fs_info (): "Get file system information. It contains the partition size, used, available, usage, and mount point information. "Tmplist = [] c = wmi. WMI () for physical_disk in c. win32_DiskDrive (): for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): for logical_disk in partition. associators ("Win32_LogicalDiskToPartition"): tmpdict = {} tmpdict ["Caption"] = logical_disk.Caption tmpdict ["DiskTotal"] = long (logical_disk.Size) /1024/1024/1024 tmpdict ["UseSpace"] = (long (logical_disk.Size)-long (logical_disk.FreeSpace)/1024/1024/1024 tmpdict ["FreeSpace"] = long (logical_disk.FreeSpace) /1024/1024/1024 tmpdict ["Percent"] = int (100.0 * (long (logical_disk.Size)-long (logical_disk.FreeSpace)/long (logical_disk.Size) tmplist. append (tmpdict) return tmplist if _ name _ = "_ main _": disk = get_disk_info () print disk print '-------------------------------------- 'fs = get_fs_info () print fs
I hope this article will help you with Python programming.