#!/usr/bin/python
#!conding=utf8
import os
from os import path
def humanbytes(B):
'Return the given bytes as a human friendly KB, MB, GB, or TB string'
B = float(B)
KB = float(1024)
MB = float(KB ** 2) # 1,048,576
GB = float(KB ** 3) # 1,073,741,824
TB = float(KB ** 4) # 1,099,511,627,776
if B < KB:
return '{0}{1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte')
elif KB <= B < MB:
return '{0:.1f}KB'.format(B/KB)
elif MB <= B < GB:
return '{0:.1f}MB'.format(B/MB)
elif GB <= B < TB:
return '{0:.1f}GB'.format(B/GB)
elif TB <= B:
return '{0:.1f}TB'.format(B/TB)
def visitDir(path):
filesize_dict={}
li = os.listdir(path)
for p in li:
pathname = os.path.join(path,p)
if not os.path.isfile(pathname): #is a dir
#print "dir:"+ pathname
visitDir(pathname)
else:
file_size_bytes = os.path.getsize(pathname)
#file_size = os.path.getsize(pathname)
file_size = humanbytes(file_size_bytes).replace(".", "_")
#print "size:" + file_size + " " + pathname
if not filesize_dict.has_key(file_size):
filesize_dict[file_size] = 1
else:
filesize_dict[file_size] += 1
for d,x in filesize_dict.items() :
print "dir:" +path + " size:" + d.replace("_",".") + " filess:"+str(x)
#print "dir:" +path + " size:" +humanbytes(d)+ " counts:"+str(x)
if __name__ == "__main__":
path = r"/mnt"
visitDir(path)
转载于:https://my.oschina.net/itfanr/blog/821382