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)