Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def format_size(size):
- """
- Formats a size in bytes.
- >>> format_size(123)
- '123 B'
- >>> format_size(123456)
- '123 kB'
- >>> format_size(123456789)
- '123 MB'
- >>> format_size(123456789012)
- '123 GB'
- >>> format_size(123456789012345)
- '123 TB'
- """
- prefixes = ['k', 'M', 'G', 'T']
- for pos, prefix in reversed(list(enumerate(prefixes))):
- base = 10 ** ((pos + 1) * 3)
- if size >= base:
- return '%i %sB' % (size / base, prefix)
- return '%i B' % size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement