Python获取Linux系统内存情况

前端之家收集整理的这篇文章主要介绍了Python获取Linux系统内存情况前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

[Python]代码    

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"))

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的Python相关文章