Linux systems use python to get memory usage information for script sharing

  • 2020-04-02 13:19:24
  • OfStack


#!/usr/bin/env Python
from __future__ import print_function
from collections import OrderedDict
def meminfo():
    ''' Return the information in /proc/meminfo
    as a dictionary '''
    meminfo=OrderedDict()
    with open('/proc/meminfo') as f:
        for line in f:
            meminfo[line.split(':')[0]] = line.split(':')[1].strip()
    return meminfo
if __name__=='__main__':
    #print(meminfo())

    meminfo = meminfo()
    print('Total memory: {0}'.format(meminfo['MemTotal']))
    print('Free memory: {0}'.format(meminfo['MemFree']))

A quick note on listing 3: listing 3 reads the information in proc/meminfo, and the split method for Python strings is still more frequent. For example, we need to store a long piece of data and store it in a structured way so that we can retrieve the data for processing later. You can use json, of course. But you can also store the data in a field and then have some kind of identifier to split it up. Strip in Python is used to remove the first character of a string, and finally listing 3 prints out the total amount of memory and the number of slots.
You can use the Python command to run the script mem.py as shown in figure 3.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201401/20140115112659.jpg? 2014015112720 ">


Related articles: