Skip to content

Instantly share code, notes, and snippets.

@romuald
Created August 26, 2015 14:47
Show Gist options
  • Save romuald/5b4daa3e310e305a2833 to your computer and use it in GitHub Desktop.
Save romuald/5b4daa3e310e305a2833 to your computer and use it in GitHub Desktop.
Convert bytes into Kbytes, MBytes & cie
#!/usr/bin/env python
"""
Converts large bytes into a more readable form.
Example::
% python mo.py 769736704
734.08 Mio - 751696.00 Kio
"""
import sys
def transform(value):
UNITS = ('o', 'Kio', 'Mio', 'Gio', 'Tio', 'Pio', 'Eio', 'Zio', 'Yio')
ret = []
for i, unit in enumerate(UNITS):
x = value / (1024.0 ** i)
if x < 1:
break
ret.append((x, unit))
return ret
def transformat(value, num=2):
return ' - '.join('%.2f %s' % z for z in reversed(transform(value)[-num:]))
for value in sys.argv[1:]:
print(transformat(int(value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment