zoukankan      html  css  js  c++  java
  • [Python Study Notes]内存信息

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    >>文件: 内存信息.py
    >>作者: liu yang
    >>邮箱: liuyang0001@outlook.com
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys,os
    import psutil
    
    
    '''将bytes数转换成更加直观的符号显示'''
    def bytes2symbols(bytes_value):
    
        '''单位符号'''
        symbols = ('K' ,'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    
        '''每个单位对应的bytes数的字典,先定义为空'''
        prefix={}
    
        '''要得到这样的{K:1024, M:1024*1024,G:1024**3},进行for循环'''
        for i,s in enumerate(symbols):
            # '''enumerate()为列举函数'''
            # '''i:代表下标'''
            # '''s:代表改下标对应的值'''
    
            # '''取到符号元组的值,作为prfix字典的key,根据key给value进行赋值'''
            prefix[s]=1024**(i+1)
    
        # '''打印得到的对应字典'''
        # print(prefix)
        symbols_value=0
        symbol=''
        # '''循环prefix字典,得到转换值'''
        for key,value in prefix.items():
            if bytes_value >=value :
                symbols_value=bytes_value/value
                symbol=key
            # 如果不满足最小的KB,则以B显示
            elif bytes_value <1024:
                return  '%0.2fB'%bytes_value
            # '''返回转换值(str)'''
        return  '%0.2f%sB'%(symbols_value,symbol)
    
    def memory_message():
        nt=psutil.virtual_memory()
        # 得到的数据:svmem(total=8482263040, available=1770676224, percent=79.1, used=6711586816, free=1770676224)
        # print(nt)
        total=bytes2symbols(nt.total)
        available=bytes2symbols(nt.available)
        percent=bytes2symbols(nt.percent)
        used=bytes2symbols(nt.used)
        free=bytes2symbols(nt.free)
    
        print("内存信息:   总内存:%4s     可使用:%4s     已使用:%4s"%(total,available,used))
    
    if __name__ == '__main__':
    
        memory_message()
    
  • 相关阅读:
    使用greenDAO遇到的问题
    使用greenDAO生成DAO代码
    Spring中Bean的生命周期
    视频弹幕开源库
    最简MacOs10.8安装
    apache-virtual host
    带删除的EditText
    替换默认debug.keystore文件
    Intellij格式化java和xml
    【数据结构】之二叉树的java实现
  • 原文地址:https://www.cnblogs.com/liu66blog/p/8449866.html
Copyright © 2011-2022 走看看