windows获取硬盘使用率等信息

 1 #coding=utf8
 2 import psutil
 3 cpu = {'user' : 0, 'system' : 0, 'idle' : 0, 'percent' : 0}
 4 mem = {'total' : 0, 'avaiable' : 0, 'percent' : 0, 'used' : 0, 'free' : 0}
 5 
 6 #磁盘名称
 7 disk_id = []
 8 #将每个磁盘的total used free percent 分别存入到相应的list
 9 disk_total = []
10 disk_used = []
11 disk_free = []
12 disk_percent = []
13 
14 #获取磁盘
15 def get_disk_info():
16     for id in psutil.disk_partitions():
17         if 'cdrom' in id.opts or id.fstype == '':
18             continue
19         disk_name = id.device.split(':')
20         s = disk_name[0]
21         disk_id.append(s)
22 
23         disk_info = psutil.disk_usage(id.device)
24         
25         disk_total.append(disk_info.total)
26         disk_used.append(disk_info.used)
27         disk_free.append(disk_info.free)
28         disk_percent.append(disk_info.percent)
29 
30 #获取CPU信息
31 def get_cpu_info():
32     cpu_times = psutil.cpu_times()
33     cpu['user'] = cpu_times.user
34     cpu['system'] = cpu_times.system
35     cpu['idle'] = cpu_times.idle
36     cpu['percent'] = psutil.cpu_percent(interval=2)
37 #获取内存信息
38 def get_mem_info():
39     mem_info = psutil.virtual_memory()
40     mem['total'] = mem_info.total
41     mem['available'] = mem_info.available
42     mem['percent'] = mem_info.percent
43     mem['used'] = mem_info.used
44     mem['free'] = mem_info.free
45 
46 if __name__ == '__main__':
47     get_cpu_info()
48     cpu_status = cpu['percent']
49     print u"CPU使用率: %s %%" % cpu_status
50     get_mem_info()
51     mem_status = mem['percent']
52     print u"内存使用率: %s %%" % mem_status
53     get_disk_info()
54     for i in range(len(disk_id)):
55         print u'%s盘空闲率: %s %%' % (disk_id[i],100 - disk_percent[i])
56     raw_input("Enter enter key to exit...")

执行结果如下:

posted on 2016-12-23 16:26  帅胡  阅读(612)  评论(0编辑  收藏  举报

导航