Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Forked from shawnbutts/bytesto.py
Last active December 28, 2015 19:29
Show Gist options
  • Save samukasmk/7550758 to your computer and use it in GitHub Desktop.
Save samukasmk/7550758 to your computer and use it in GitHub Desktop.
def bytes_converter(bytes, from_unit, to_unit, bsize=1024):
"""
Converted bytes units
>>> bytes_converter('2', 'gigabyte', 'kilobyte')
2097152.0
>>> bytes_converter('2097152', 'kilobyte', 'gigabyte')
2.0
"""
units = {
'bit': 1,
'byte': 2,
'kilobyte': 3,
'megabyte': 4,
'gigabyte': 5,
'terabyte': 6,
'petabyte': 7,
'exabyte': 8,
'zettabyte': 9,
'yottabyte': 10,
}
data = float(bytes)
if units[from_unit] < units[to_unit]:
for i in range(units[to_unit] - units[from_unit]):
#print str(i)+' -> '+str(data)+' / '+ str(bsize)
data = data / bsize
else:
for i in range(units[from_unit] - units[to_unit]):
#print str(i)+' -> '+str(data)+' * '+ str(bsize)
data = data * bsize
return(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment