Print human readable file size.

Often you have to print a file or a directory size. Here is a small recipe to print a file, or a directory size easily readable by humans.

def sizeof_fmt(num):
    for x in ['bytes','KB','MB','GB','TB']:
        if num < 1024.0:
            return "%3.1f%s" % (num, x)
        num /= 1024.0

def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB','TB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0

Example:
>>> sizeof_fmt(168963795964)
'157.4GB'

>>> sizeof_fmt(168963795964)
'157.4GB'
 

Comments

Posted by: Justin Dec 12, 2007 @ 19:50

Don't you want to use the upper-case B to indicate that these sizes are Bytes and not bits?

Posted by: Fred Dec 13, 2007 @ 08:17

Thanks for your comment. I have changed the code.

Posted by: Tom Jan 18, 2008 @ 17:13

Handy function, saved me figuring it out myself, thanks :)

Posted by: florist japan Dec 29, 2009 @ 03:57

this is aweosme

Posted by: laptop battery Dec 30, 2009 @ 22:45

Everything will be all right,I am behind you.

Posted by: fake rolex watches Jan 04, 2010 @ 23:41

Welcome to our website,and you will enjoy the sunshine service. Let's

abandon all the worry,and enjoy the happy shopping travel.All the

replica watches paypal and replica watches are most in fashion

boutique this year.rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament. In addtin, the

fake watches paypal and the other fake rolex paypal make you mold

your unique glamour this winter.The swiss rolex replica have the high

quality but low price. replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches. Enjoying the

fun of net purchase! replica rolex We will supply you the perfect

service and after sale support system. Our honest will bring you

absolute satisfaction.

Posted by: evening dresses Jan 09, 2010 @ 04:01

The blog is really informative and i like to share it with my friends we can easily learn more about web designing through this post thanks for post

Posted by: film izle Jan 17, 2010 @ 04:53

Thank you for sharing your friends. Hope to see you another day.

Posted by: divx film izle Jan 17, 2010 @ 04:55

Thanks for your good website and for sharing your experiences.

Posted by: Wholesale Jan 17, 2010 @ 21:59

in fact,to say nothing more on this subject, but still would like to thank for sharing

Posted by: links of london Jan 19, 2010 @ 00:26

replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches.

Posted by: ed hardy Feb 05, 2010 @ 21:28

I am a Mozilla Firefox fan too and I have been using it for the last 6 years or so.I mainly like its extended features and its user friendly interface.

Posted by: Chris C Feb 17, 2010 @ 09:18

Nice, but the function wouldn't return anything if the filesize was bigger than 1024TB. I know it's a silly high number, but perhaps to futureproof it, maybe at the bottom outside the for loop have a return value there of numPB? :)

Posted by: evening dresses Feb 19, 2010 @ 06:05

I look forward to reading more from you.

Posted by: Nike Air Max Mar 04, 2010 @ 01:02

So beautiful sharing!Thank you very much.

Posted by: tag heuer watch Mar 04, 2010 @ 23:16

can show a man not only his wealthy but also his high taste.tag heuer watch As the society developed, many people are pay more attention to one's wearing than before, especially a watch. tag heuer watch If you are wearing a famous brand watch, people will look up you, also will love to do business with you. You will be a successful man with a such luxury fashion watch.

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image