#!/usr/bin/python
#*-*coding:utf8*-*
"""適用于在/proc/meminfo中提供了可用內存MemAvailable的系統使用"""
from optparse import OptionParser
import sys
parser = OptionParser()
parser.add_option("-k",
dest="k",
action="store_true",
default=True,
help="Display Meminfo KB")
parser.add_option("-m",
dest="m",
action="store_true",
default=False,
help="Display Meminfo MB")
parser.add_option("-g",
dest="g",
action="store_true",
default=False,
help="Display Meminfo GB")
option, args = parser.parse_args()
with open("/proc/meminfo") as meminfo:
for i in meminfo:
if i.startswith("MemTotal"):
total_mem = i.split()
total_mem = total_mem[1]
continue
elif i.startswith("MemAvailable"):
free_mem = i.split()
free_mem = free_mem[1]
break
else:
pass
total_mem, free_mem = float(total_mem), float(free_mem)
x = free_mem/total_mem
if option.k and (not option.m) and (not option.g):
print("總內存為:%d" %total_mem + " kB")
print("剩余可用內存為:%d" %free_mem + " kB")
print("剩余內存可用比例為:%d" %(x*100) + "%" )
elif option.k and option.m and (not option.g):
print("總內存為:%d" %(total_mem/1024) + " MB")
print("剩余可用內存為:%d" %(free_mem/1024) + " MB")
print("剩余內存可用比例為:%d" %(x*100) + "%" )
elif option.k and (not option.m) and option.g:
print("總內存為:%.2f" %(total_mem/1024/1024) + " GB")
print("剩余可用內存為:%.2f" %(free_mem/1024/1024) + " GB")
print("剩余內存可用比例為:%d" %(x*100) + "%" )
else:
pass
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
