• python统计磁盘使用情况


    #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 ";
            elif item[1] > 1024:
                strTmp = item[0]+" "+str(int(float(item[1]/1024)))+"MB ";                           
            else:
                strTmp = item[0]+" "+str(item[1])+"KB ";
                                         
            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============");


  • 相关阅读:
    宏定义define和const的区别
    C++11新特性(1)
    TCP协议
    sql查询 不等于的坑null
    To B市场品牌实战课 用产品故事引爆市场
    Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中
    极客时间返现小程序,全网最优惠返现最多
    分布式数据库,从功能特性到运行机制,讲透本质
    生财有术知识星球整理
    ff
  • 原文地址:https://www.cnblogs.com/franjia/p/4384253.html
Copyright © 2020-2023  润新知