python中查看操作系统基本信息的操作

橙三吉。 提交于 2020-01-24 20:26:13

1. 返回操作系统类型

posix 表示linux操作系统
nt 表示windows操作系统

在这里插入图片描述

2. 操作系统详细信息

在这里插入图片描述在这里插入图片描述

3. 环境变量

在这里插入图片描述在这里插入图片描述

4. 获取一段时间内CPU的占有率

import psutil
import time

# cpu_res = psutil.cpu_percent()
# print(cpu_res)

# 每一秒获取获取cpu的占有率 --->持久化保存
# 如何将时间和对应的cpu占有率去匹配

while True:
    # 获取当前时间和cpu的占有率
    t = time.localtime()
    cpu_time = '%d:%d:%d' %(t.tm_hour,t.tm_min,t.tm_sec)
    cpu_res = psutil.cpu_percent()
    print(cpu_res)

    # 保存在文件中
    with open('cpu.txt','a+') as f:
        f.write('%s %s \n' %(cpu_time,cpu_res))
    time.sleep(1)

在这里插入图片描述
在这里插入图片描述
还可以使用pyecharts模块绘制图形

import random
from pyecharts.charts import Line
import pyecharts.options as opts

# 获取折线图需要绘制的数据信息;
x = []
y = []

with open('cpu.txt') as f:  # 以读的方式打开文件
    for line in f:          # 依次遍历文件的每一行内容
        time, per = line.split()    # 返回时间和对应时间
        # 的cpu占有率
        x.append(time)
        y.append(per)

# 添加x和y对应的点;
line = (
     Line()
    .add_xaxis(x)
    .add_yaxis("", y)

    .set_global_opts(title_opts=opts.
                     TitleOpts(title="Cpu占有率散点图"))
)
# 将折线图信息保存到文件中;
line.render()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!