Python-基础练习题1

计算系统内存情况: 

#!/usr/bin/env python
 
with open('/proc/meminfo') as fd:
  while True:
  line = fd.readline()
    if line.startswith('MemTotal'):
      total = line.split()[1]
      continue
    if line.startswith('MemFree'):
      used = line.split()[1]
      break
  print 'total:%s' % (int(total)/1024)+'M'
  print 'used:%s' % (int(used)/1024)+'M'
  print 'usege:%.2s%%' % (float(used)/float(total)*100)

 

line.startswith('string')
  判断字符串是否是line的开头,(功能等同于^string)
line.split()
  把字符串以空格分隔成列表,line.split()[1],表示取列表的第二个元素
 
由于python中int除以int,就算除不尽也不会计算小时,所以这里我们把int转换成float型
posted @ 2016-11-21 19:03  SpeicalLife  阅读(153)  评论(0)    收藏  举报