码迷,mamicode.com
首页 > 编程语言 > 详细

python代码学习-监控WIN磁盘脚本

时间:2016-01-19 12:19:29      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

安装wmi,pywin32模块

# -*- 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() 
     for x in disk:print x 
     print -------------------------------------- 
     fs = get_fs_info() 
     for x in fs:print x

得到的结果是

{‘Caption‘: u‘TOSHIBA THNSNJ128GCSU‘, ‘Size‘: 119L}
{‘Caption‘: u‘WDC WD5000LPVX-22V0TT0‘, ‘Size‘: 465L}
--------------------------------------
{‘Caption‘: u‘C:‘, ‘Percent‘: 47, ‘UseSpace‘: 47L, ‘DiskTotal‘: 100L, ‘FreeSpace‘: 52L}
{‘Caption‘: u‘D:‘, ‘Percent‘: 60, ‘UseSpace‘: 60L, ‘DiskTotal‘: 100L, ‘FreeSpace‘: 39L}
{‘Caption‘: u‘E:‘, ‘Percent‘: 74, ‘UseSpace‘: 185L, ‘DiskTotal‘: 250L, ‘FreeSpace‘: 64L}
{‘Caption‘: u‘F:‘, ‘Percent‘: 70, ‘UseSpace‘: 81L, ‘DiskTotal‘: 115L, ‘FreeSpace‘: 34L}

 

python代码学习-监控WIN磁盘脚本

标签:

原文地址:http://www.cnblogs.com/olinux/p/5141453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!