Skip to content

Instantly share code, notes, and snippets.

@ElectroDrome
Last active April 29, 2024 20:10
Show Gist options
  • Save ElectroDrome/2cc0193d47555125dfbc0b34a4107b0a to your computer and use it in GitHub Desktop.
Save ElectroDrome/2cc0193d47555125dfbc0b34a4107b0a to your computer and use it in GitHub Desktop.
Python: Format a number of bytes into a human readable format (non looping solution)
def format_bytes(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']):
return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:]) if units[1:] else f'{bytes>>10}ZB'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment