Bytes to Human Readable

Convert bytes to human readable format (KB, MB, GB, etc.) for file sizes.

Code

Utilities
units = ['B', 'KB', 'MB', 'GB', 'TB']
i = 0
size = bytes
while size >= 1024 and i < len(units) - 1:
    size /= 1024
    i += 1
f"{size:.2f} {units[i]}"

Parameters

Number of bytes.

Server

More Python Snippets