import psutil
内存
mem = psutil.virtual_memory() print(mem) >>> svmem(total=17048064000, available=13197590528, percent=22.6, used=3850473472, free=13197590528) mem_size = int(mem[0])/1024/1024/1024 print("内存总大小是%.0fG"%mem_size) >>> 内存总大小是16G mem_use = int(mem[3])/1024/1024/1024 mem_use_rate = int(mem[3]) / int(mem[0]) *100 print("占用内存%.2fG,占用率%.2f%%"%(mem_use,mem_use_rate)) >>> 占用内存3.59G,占用率22.59% mem_free = int(mem[4])/1024/1024/1024 print("内存剩余%.2fG"%mem_free) >>> 内存剩余12.29G
硬盘
disk1 = psutil.disk_partitions(all) print("你的磁盘状况如下:") i = 0 for b in disk1: b = disk1[i][0] disk2 = psutil.disk_usage(b) disk_rate = float(disk2.used / disk2.total *100) print("%s 总空间%.2f,剩余空间%.2f,使用率%.2f%%"%(b,float(disk2.total/1024/1024/1024),float(disk2.free/1024/1024/1024),float(disk_rate))) i += 1 >>> 你的磁盘状况如下: >>> C: 总空间138.89,剩余空间80.40,使用率42.11% >>> D: 总空间136.80,剩余空间51.44,使用率62.40% >>> E: 总空间100.00,剩余空间35.72,使用率64.28% >>> F: 总空间100.00,剩余空间40.66,使用率59.34%
cpu
cpu = psutil.cpu_percent(interval=True) print(cpu) cpu1 = psutil.cpu_count() print(cpu1) >>> 2.9 >>> 8