iterating in reverse
Paul Rubin
phr-n2003b at NOSPAMnightsong.com
Fri Jan 17 18:38:39 EST 2003
Nick Vargish <nav at adams.patriot.net> writes:
> I'm summing the number of bytes used in a file repository (I wish I
> could just use "du", but alas...), so negative numbers are not a
> factor. But I like the generator approach, very tricky.
How about something like (untested):
def convert(nbytes):
suffixes = ['K', 'M', 'G', 'T', 'P', 'E']
s = ''
while nbytes >= 1024:
nbytes /= 1024.0
s = suffixes.pop(0)
return '%.3f%c'% (nbytes, s)
More information about the Python-list
mailing list