linux系统使用python获取内存使用信息脚本分享
时间:2022-10-14 07:50:53
#!/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']))
简单说明一下清单 3:清单 3 读取 proc/meminfo 中的信息,Python 字符串的 split 方法是用的频率还是比较多的。比如我们需要存储一个很长的数据,并且按照有结构的方法存储,方便以后取数据进行处理。当然可以用 json 的形式。但是也可以把数据存储到一个字段里面,然后有某种标示符来分割。 Python 中的 strip 用于去除字符串的首位字符,最后清单 3 打印出内存总数和空闲数。
可以使用 Python 命令运行脚本 mem.py 结果见图 3。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Django教程之实现天气应用程序
![](https://img.aspxhome.com/file/2023/1/69011_0s.png)
关于select元素的两个小问题
使用pyecharts在jupyter notebook上绘图
![](https://img.aspxhome.com/file/2023/9/113139_0s.jpg)
Python 命令行解析工具 argparse基本用法
![](https://img.aspxhome.com/file/2023/3/59283_0s.jpg)
Oracle数据安全面面观
如何清除Vbscript惹出来的中文乱码?
纯ASP结合VML生成完美图-柱图
pandas groupby 分组取每组的前几行记录方法
如何写一个通用的JavaScript效果库!(2/2)
python+django+selenium搭建简易自动化测试
![](https://img.aspxhome.com/file/2023/3/75903_0s.png)
Python在for循环中更改list值的方法【推荐】
VUE3中watch监听使用实例详解
用ASP建立一个简单的聊天室
Python基于requests实现模拟上传文件
![](https://img.aspxhome.com/file/2023/4/85064_0s.png)
基于python list对象中嵌套元组使用sort时的排序方法
javascript实现切割轮播效果
![](https://img.aspxhome.com/file/2023/9/56569_0s.jpg)
Python扫描IP段查看指定端口是否开放的方法
Numpy中创建数组的9种方式小结
![](https://img.aspxhome.com/file/2023/7/103247_0s.png)
Python配置虚拟环境图文步骤
![](https://img.aspxhome.com/file/2023/8/133808_0s.png)