Bytes/File Size Format Function

half.italian at gmail.com half.italian at gmail.com
Wed Jun 13 09:53:18 CEST 2007


On Jun 13, 12:48 am, half.ital... at gmail.com wrote:
> On Jun 12, 8:47 pm, samuraisam <samuraib... at gmail.com> wrote:
>
>
>
> > 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)]
> >     )
>
> Wait a sec...what if you send it a large amount of bytes? Say...
> bigger than 2147483647.  You'll get an OverflowError.   I thought you
> had my solution...
>
> ~Sean





More information about the Python-list mailing list