[Python Study Notes] Memory information

Source: Internet
Author: User

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.