human_file_size

human_file_size(size_in_bytes: int | float) str[source]

Converts a file size in bytes to a human-readable string format.

Uses binary prefixes (KB, MB, GB, TB, PB, EB, ZB, YB) where 1 KB = 1024 bytes.

Parameters:

size_in_bytes -- The size in bytes (integer or float).

Returns:

A string representing the human-readable file size (e.g., "1.2 KB", "3.45 MB").

Raises:

ValueError -- If the input size_in_bytes is negative.

Examples

>>> bytes_to_human_readable(0)
'0 Bytes'
>>> bytes_to_human_readable(500)
'500 Bytes'
>>> bytes_to_human_readable(1024)
'1.0 KB'
>>> bytes_to_human_readable(1500)
'1.46 KB'
>>> bytes_to_human_readable(1024 * 1024)
'1.0 MB'
>>> bytes_to_human_readable(2500000)
'2.38 MB'
>>> bytes_to_human_readable(1024**3 * 1.5)
'1.5 GB'
>>> bytes_to_human_readable(1024**6)
'1.0 EB'