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