练习11:将时间和对应的cpu占有率匹配

import psutil
import time

while True:
    # 获取当前时间和cpu的占有率
    t = time.localtime()  # 元组时间
    cpu_time = '%d:%d:%d' %(t.tm_hour,t.tm_min,t.tm_sec)
    # 每一秒获取获取cpu的占有率
    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)

在这里插入图片描述

发布了106 篇原创文章 · 获赞 1 · 访问量 2380

猜你喜欢

转载自blog.csdn.net/weixin_43384009/article/details/104092160