获取Linux系统内存情况

2015-09-08 0 303
获取Linux系统内存情况
import subprocess
import re

keydic = {"MemTotal":"总内存(单位G)",
          "MemFree":"剩余内存(单位G)",
          "MemAvailable":"可用内存(单位G)",
          "Cached":"缓存内存(单位G)"}

def command(command):
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    resultDic = {}
    for line in p.stdout.readlines():
        line = str(line,encoding="utf-8")
        result = re.split("\s*",line)
        if result[0][:-1] in keydic:
            resultDic[keydic[result[0][:-1]]] = "%.2f" %(int(result[1])/(1024**2))
    return resultDic

if __name__ == "__main__":
   print(command("cat /proc/meminfo"))

遇见资源网 python 获取Linux系统内存情况 http://www.ox520.com/15765.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务