fork(5) download
  1. def convert_size_to_bytes(size):
  2. multipliers = {
  3. 'kb': 1024,
  4. 'mb': 1024*1024,
  5. 'gb': 1024*1024*1024,
  6. 'tb': 1024*1024*1024*1024
  7. }
  8.  
  9. for suffix in multipliers:
  10. if size.lower().endswith(suffix):
  11. return int(size[0:-len(suffix)]) * multipliers[suffix]
  12. else:
  13. if size.lower().endswith('b'):
  14. return int(size[0:-1])
  15.  
  16. try:
  17. return int(size)
  18. except ValueError: # for example "1024x"
  19. print('Malformed input!')
  20. exit()
  21.  
  22. print(convert_size_to_bytes("32MB"))
  23. print(convert_size_to_bytes("100kB"))
  24. print(convert_size_to_bytes("123B"))
  25. print(convert_size_to_bytes("123"))
  26. print(convert_size_to_bytes("123x"))
Success #stdin #stdout 0.02s 28376KB
stdin
Standard input is empty
stdout
33554432
102400
123
123
Malformed input!