python統計磁碟使用方式

來源:互聯網
上載者:User

標籤:

#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統計磁碟使用方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.