博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python统计磁盘使用情况
阅读量:6857 次
发布时间:2019-06-26

本文共 1300 字,大约阅读时间需要 4 分钟。

#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============");

转载于:https://www.cnblogs.com/franjia/p/4384253.html

你可能感兴趣的文章
Purism 发布 PureBoot:高度安全、完整的 Linux 引导流程
查看>>
poj2891:Strange Way to Express Integers
查看>>
阿里云发布智能语音自学习平台,零基础训练人工智能模型
查看>>
想成为软件架构师,一定要看的经典书单
查看>>
真正理解拉格朗日乘子法和 KKT 条件
查看>>
梓论坛 zibbs 1.0 发布,PHP 轻论坛系统
查看>>
阿里云专访Redisson作者Rui Gu:构建开源企业级Redis客户端之路
查看>>
读书笔记 effective c++ Item 55 让你自己熟悉Boost
查看>>
【小工匠聊Modbus】08-功能代码01H
查看>>
高可靠性、超大规模、极致性能、灵活部署的阿里云Apsara Block Storage,你pick嘛?...
查看>>
Mealy和moore型状态机的主要区别
查看>>
Linux环境下安装mysql并分配用户权限
查看>>
VR视频创作,想说爱你不容易
查看>>
笨办法学 Python · 续 练习 23:三叉搜索树
查看>>
可穿戴设备不能急于戴上“智能”的高帽子
查看>>
扒一扒政府的专用黑客组织ZooPark
查看>>
Ubuntu下Deb软件包相关安装与卸载
查看>>
互联网转型需要微服务架构
查看>>
Airbus Safran Launchers:选择西门子彻底改革产品生命周期流程
查看>>
文本数据的分分合合
查看>>