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