linux系统使用python获取内存使用信息脚本分享

480次阅读  |  发布于5年以前

复制代码 代码如下:

!/usr/bin/env Python

from future import print_function
from collections import OrderedDict

def meminfo():
''' Return the information in /proc/meminfo
as a dictionary '''
meminfo=OrderedDict()

with open('/proc/meminfo') as f:  
    for line in f:  
        meminfo[line.split(':')[0]] = line.split(':')[1].strip()  
return meminfo

if name=='main':

print(meminfo())

meminfo = meminfo()  
print('Total memory: {0}'.format(meminfo['MemTotal']))  
print('Free memory: {0}'.format(meminfo['MemFree']))  

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8