Bytes/File Size Format Function
samuraisam
samuraiblog at gmail.com
Tue Jun 12 23:47:52 EDT 2007
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
More information about the Python-list
mailing list