python 调用linux系统命令

来源:互联网 发布:日本语言翻译软件 编辑:程序博客网 时间:2024/06/01 09:51

        是和lcc讨论”画饼“问题的后续。很简单的一段,调用df -hl 查看磁盘使用情况,小脚本把单位归一化了。

#! usr/bin/python2.7# -*- coding: utf-8 -*-#2014-04-17 lyimport 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.0elif 'K'==c:num=float(b)/(1024.0**2)else:num=0return numf=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)) 


0 0
原创粉丝点击