понедельник, 1 апреля 2013 г.

Python - filesize in bytes with proper prefix


 
def get_bytes_size_units(num):
  """ Get file size with proper units of measurement.
  """
  range_titles = ["b", "kB", "Mb", "Gb", "Tb", "PB", "EB", "ZP", "YB"]
  range_titles_len = len(range_titles)
  i = 1
  prev_value = 0
  new_value = num
  is_iteration = True
  while is_iteration:
    prev_value = new_value
    new_value = num >> ((i << 1) * 5)
    i += 1
    is_iteration = new_value < 0 and i < range_titles_len
  i -= 2
  return "%.2f%s" % (float(num) / float(1 << ((i << 1) * 5)), range_titles[i])

Комментариев нет:

Отправить комментарий