def to_bytes(text, default=0):
 BYTE_MULTIPLIERS = {'': 1, 't': (1024 ** 4), 'g': (1024 ** 3), 'm': (1024 ** 2), 'k': 1024}
 mult_key_org = text.lstrip('-1234567890')
 mult_key = mult_key_org.lower()
 mult_key_len = len(mult_key)
 if mult_key.endswith('b'):
  mult_key = mult_key[0:(-1)]
 try:
  multiplier = BYTE_MULTIPLIERS[mult_key]
  if mult_key_len:
   text = text[0:(- mult_key_len)]
  return (int(text) * multiplier)
 except KeyError:
  msg = (_('Unknown byte multiplier: %s') % mult_key_org)
  raise TypeError(msg)
 except ValueError:
  return default

def to_bytes ( text , default = 0 ) : BYTE_MULTIPLIERS = { '' : 1 , 't' : ( 1024 ** 4 ) , 'g' : ( 1024 ** 3 ) , 'm' : ( 1024 ** 2 ) , 'k' : 1024 } mult_key_org = text . lstrip ( '-1234567890' ) mult_key = mult_key_org . lower ( ) mult_key_len = len ( mult_key ) if mult_key . endswith ( 'b' ) : mult_key = mult_key [ 0 : ( - 1 ) ] try : multiplier = BYTE_MULTIPLIERS [ mult_key ] if mult_key_len : text = text [ 0 : ( - mult_key_len ) ] return ( int ( text ) * multiplier ) except KeyError : msg = ( _ ( 'Unknown byte multiplier: %s' ) % mult_key_org ) raise TypeError ( msg ) except ValueError : return default
make sure text is bytes type .