python使用wmi模块获取windows下硬盘信息的方法

本文实例讲述了python使用wmi模块获取windows下硬盘信息的方法。分享给大家供大家参考。具体实现方法如下:

# -*- coding: utf-8 -*-
#import
########################################################################
import os, sys
import time
import wmi
########################################################################
#function
########################################################################
def get_disk_info():
“””
获取物理磁盘信息。
“””
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() :
“””
获取文件系统信息。
包含分区的大小、已用量、可用量、使用率、挂载点信息。
“””
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

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论