• Python3 获取系统资源


    cpu disk mem

    import os
    import psutil
    os.chdir(os.getcwd())
    #cpu
    def get_cpu_info():
        cpu_percent=psutil.cpu_percent(interval=1)
        cpu_info="used cpu perence is : %0.2f%%" % cpu_percent,
        #print("used cpu perence is : %0.2f%%" % cpu_percent)
        return cpu_info
    #memory
    def get_memory_info():
        virtual_memory = psutil.virtual_memory()
        used_memory = virtual_memory.used / 1024 / 1024
        free_memory = virtual_memory.free / 1024 / 1024
        memory_percent = virtual_memory.percent
        #print("memory perence is:%0.2f%%" % memory_percent,"usedmemory is:%0.2f M" % used_memory)
        mem_info="memory perence is:%0.2f%%" % memory_percent,"usedmemory is:%0.2f M" % used_memory
        return mem_info
    
    #disk
    def get_disk_info():
        # 循环磁盘分区
        content = ""
        for disk in psutil.disk_partitions():
            # 读写方式 光盘 or 有效磁盘类型
            if 'cdrom' in disk.opts or disk.fstype == '':
                continue
            disk_name_arr = disk.device.split(':')
            disk_name = disk_name_arr[0]
            disk_info = psutil.disk_usage(disk.device)
            # 磁盘剩余空间,单位G
            free_disk_size = disk_info.free//1024//1024//1024
            # 当前磁盘使用率和剩余空间G信息
            d_info = "%s: disk_percent:%s%%,disk_free:%iG" % (disk_name,str(disk_info.percent),free_disk_size)
            #if disk_name == "C" :
            #    return d_info
            #    print(d_info)
            # 拼接多个磁盘的信息
            content = content + d_info
        #print(content)
        return content
    

      

  • 相关阅读:
    记录一些笔记~
    JS里的居民们7-对象和数组转换
    JS里的居民们6-数组排序
    JS里的居民们5-数组(栈)
    MySQL设置当前时间为默认值的方法
    session过期问题
    MyIsam和InnoDB的区别
    ajax请求 json格式和数组格式总结
    wamp 2.5 开放访问权限和设置虚拟域名
    checkbox属性checked="checked"通过js已设置,但是不勾选
  • 原文地址:https://www.cnblogs.com/cyanrose/p/11882992.html
Copyright © 2020-2023  润新知