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'
|
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 Example: >>> 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 :)