码迷,mamicode.com
首页 > 编程语言 > 详细

python统计磁盘使用情况

时间:2015-04-01 17:11:38      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

#coding:utf-8
import os;

def SortList(item):
    return item[1];

def ReadSize(fileName):
    return float(os.path.getsize(fileName));

def WriteAll(path):
    l = []
    loger = open("test.log","w");
    writer = open("path.txt","w");
    reader = open("path.txt","r");
    size = 0;
    for root,dirs,files in os.walk(path):
        for filesPath in files:
            try:
                fllePath = os.path.join(root,filesPath);
                fileSize = float(ReadSize(fllePath)/1024);
                size += fileSize;
                x = (fllePath,int(fileSize));
                l.append(x);
            except:
                loger.write("读取:"+os.path.join(root,filesPath)+"文件大小失败!");
                
                continue;
    l = sorted(l,key=SortList,reverse=True);
    
    for item in l:
        strTmp = "";
        if float(item[1]/1024) > 1024:
            strTmp = item[0]+" "+str(int(float(item[1]/1024/1024)))+"GB\n";
        elif item[1] > 1024:
            strTmp = item[0]+" "+str(int(float(item[1]/1024)))+"MB\n";                           
        else:
            strTmp = item[0]+" "+str(item[1])+"KB\n";
                                     
        writer.write(strTmp);
    
    a = round(float(size/1024/1024),2)   #四舍五入取小数点后两位
    writer.write("共使用磁盘空间:"+str(a)+"GB");
    
    print "共使用磁盘空间:"+str(a)+"GB"
    loger.close();
    writer.close();   
    ‘‘‘print(reader.read());‘‘‘
    reader.close();

#当前目录
#fileName = os.getcwd();
    
print "D盘已使用空间: "
fileName = ‘D:/‘;
WriteAll(fileName);

print "E盘已使用空间: "
fileName = ‘E:/‘;
WriteAll(fileName);
print "end 请核实"
#raw_input("============END============");


python统计磁盘使用情况

标签:

原文地址:http://www.cnblogs.com/franjia/p/4384253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!