Python3 获取系统资源

徘徊边缘 提交于 2019-12-04 20:51:54

cpu disk mem

 

import osimport psutilos.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

  

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!