Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1.  
  2. def format_size(size):
  3. """
  4. Formats a size in bytes.
  5.  
  6. >>> format_size(123)
  7. '123 B'
  8. >>> format_size(123456)
  9. '123 kB'
  10. >>> format_size(123456789)
  11. '123 MB'
  12. >>> format_size(123456789012)
  13. '123 GB'
  14. >>> format_size(123456789012345)
  15. '123 TB'
  16. """
  17. prefixes = ['k', 'M', 'G', 'T']
  18. for pos, prefix in reversed(list(enumerate(prefixes))):
  19. base = 10 ** ((pos + 1) * 3)
  20. if size >= base:
  21. return '%i %sB' % (size / base, prefix)
  22. return '%i B' % size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement