"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> Memory Information .py>> Author: Liu Yang>> e-mail: [email protected] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "#!/usr/bin/env python#-*-coding:utf-8-*-import sys,osimport psutil "' Convert bytes number to more intuitive symbol display ' ' Def bytes2symbols (bytes_value): ' Unit symbol ' ' symbols = (' K ', ' M ', ' G ', ' T ', ' P ', ' E ', ' Z ', ' Y The dictionary of the number of bytes per unit, defined as null ' prefix={} ' to get such {k:1024, m:1024*1024,g:1024**3}, for Loop ' for i,s in Enume Rate (Symbols): # ' Enumerate () for enumeration function ' # ' I: for subscript ' # ' s: Represents the value of the subscript corresponding to ' ' # ' to the value of the symbol tuple as a PRF The key of the IX dictionary, which is assigned value according to key "' prefix[s]=1024** (i+1) # '" "Print the corresponding Dictionary" "" "" "" "" "" "(prefix) symbols_value=0 sym Bol= ' # ' Loop prefix dictionary, get converted value ' for Key,value in Prefix.items (): If Bytes_value >=value:symbo Ls_value=bytes_value/value Symbol=key # If the minimum KB is not met, display elif bytes_value <1024:return '%0.2fb '%bytes_value # "in B to return the converted value (str) "Return '%0.2F%SB '% (symbols_value,symbol) def memory_message (): Nt=psutil.virtual_memory () # Data obtained: Svmem (total =8482263040, available=1770676224, percent=79.1, used=6711586816, free=1770676224) # Print (NT) total=bytes2symbols (n T.total) Available=bytes2symbols (nt.available) percent=bytes2symbols (nt.percent) Used=bytes2symbols (nt.used) F Ree=bytes2symbols (nt.free) print ("Memory Information: Total Memory:%4s available:%4s used:%4s"% (total,available,used)) if __name__ = = ' __mai N__ ': Memory_message ()
[Python Study Notes] Memory information