import psutil
def getMyCpu():
data = psutil.virtual_memory()
total = data.total # 总内存
print('TotalMemory', total)
free = data.available # 可用内存
print('FreeMemory', free)
memory = "Memory usage:%d" % (int(round(data.percent)))
print(memory + "%")
CPU = "CPU:%0.2f" % psutil.cpu_percent(interval=1)
print(CPU + '%')
RestMemory = 100 - int(round(data.percent))
if RestMemory > 10:
print("\033[0;34m%s\033[0m" % "Sufficient Memory")#输出蓝色字体
elif RestMemory <= 5:
print("\033[0;31m%s\033[0m" % "AvailableMemory<=5%")#输出红色字体
else:
print("\033[0;32m%s\033[0m" % "AvailableMemory<=10%")#输出绿色字体
if __name__ == "__main__":
getMyCpu()
输出结果为:
TotalMemory 8482037760
FreeMemory 3632578560
Memory usage:57%
CPU:5.10%
Sufficient Memory