是和lcc讨论”画饼“问题的后续。很简单的一段,调用df -hl 查看磁盘使用情况,小脚本把单位归一化了。
#! usr/bin/python2.7
# -*- coding: utf-8 -*-
#2014-04-17 ly
import os
def Gb(a):
n=len(a)
b=a[0:n-1]
c=a[n-1]
if 'G'==c:
num=float(b)
elif 'M'==c:
num=float(b)/1024.0
elif 'K'==c:
num=float(b)/(1024.0**2)
else:
num=0
return num
f=os.popen("df -hl")
use=[]
notuse=[]
line=f.readline()
line=f.readline()
while line:
data=line.split()
a=Gb(data[2])
b=Gb(data[3])
use.append(a)
notuse.append(b)
line=f.readline()
print "yeah"
a=sum(use)
b=sum(notuse)
print "the total volume of the disk is:"+str(a+b)
print str(a/(a+b)*100)+"% has been used"
f=open("1.txt",'w')
f.write(str(a)+'\n')
f.write(str(b))