Sunday, November 11, 2012

Human readable file/memory sizes v2 (Python)

Human readable file/memory sizes v2 (Python):
Python
recipe 578323

by Tony Flury


(formatting, memory, size).





In writing a application to display the file sizes of set of files, I wanted to provide a human readable size rather then just displaying a byte count (which can get rather big).

I developed this useful short recipe that extends the format specifier mini Language to add new presentation type s- which will intelligently convert the value to be displayed into a known human readable size format - i.e. b, Kb,Mb, Gb, B, KB etc. It honours the rest of the format specification language (http://docs.python.org/2/library/string.html#format-string-syntax)

It uses a factor of 1024 for IEC and common formats, and factor of 1000 for SI units.

No comments:

Post a Comment