Copy Code code as follows:
#!/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 '))
In a nutshell, Listing 3: Listing 3 reads the information in Proc/meminfo, and the split method of the Python string is more frequently used. For example, we need to store a very long data, and in accordance with the structure of the method of storage, to facilitate the subsequent processing of data. Of course you can use the form of JSON. But you can also store the data in a field, and then have some sort of identifier to split. The strip in Python is used to remove the first character of the string, and the last listing 3 prints out the total number of memory and the number of idle.
You can run the script using the Python command mem.py the results are shown in Figure 3.