Linux system uses Python to get memory use information script sharing _python

Source: Internet
Author: User

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.

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.