#!/usr/bin/python
With open ('/proc/meminfo ') as FD:
For line in FD:
If Line.startswith (' Memtotal '):
Total = Line.split () [1]
Continue
If Line.startswith (' Memfree '):
Free = Line.split () [1]
Break
freemem = Int (free)/1024.0
Totalmem = Int (total)/1024.0
Print "Freemem:" + "%.2f"% freemem+ ' M '
Print "Totalmem:" + "%.2f"% totalmem+ ' M '
Print "Freemem/totalmem:" + "%.2f"% ((FREEMEM/TOTALMEM) *100) + '% '
The results of the implementation are as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/05/wKioL1c0sISTe-qgAAAeJ0GpVPA169.png "title=" 1.png " alt= "Wkiol1c0siste-qgaaaej0gpvpa169.png"/>
Description
1, withopen ('/proc/meminfo ') as FD: and fd=open ('/proc/meminfo ') execution of the same results , are traversing the file, the former only in Python 2.6 and later, the latter when the open file reaches a few g, it consumes memory, while the former does not have this problem.
2. String method:
Line.startswith : What starts with, returns a Boolean value (True/false)
Line.split : separated by a space or TAB key to form a list of values (strings) indexed by the list index
3,"%.2f" and "%s " are formatted string output, the former output is reserved two decimal places
This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1772888
Python: Statistical system remaining memory