I am trying to write an application to convert bytes to kb to mb to gb to tb. Here\'s what I have so far:
def size_format(b): if b < 1000: r
Fixed version of Bryan_Rch's answer:
def format_bytes(size): # 2**10 = 1024 power = 2**10 n = 0 power_labels = {0 : '', 1: 'kilo', 2: 'mega', 3: 'giga', 4: 'tera'} while size > power: size /= power n += 1 return size, power_labels[n]+'bytes'