from math import log2


def human_readable_file_size(size):
    """
    Returns a human readable file size string for a size in bytes. 
    
    Adapted from https://stackoverflow.com/a/25613067)
    """

    _suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']

    # determine binary order in steps of size 10 
    # (coerce to int, // still returns a float)
    order = int(log2(size) / 10) if size else 0
    # format file size
    # (.4g results in rounded numbers for exact matches and max 3 decimals, 
    # should never resort to exponent values)
    return '{:.4g} {}'.format(size / (1 << (order * 10)), _suffixes[order])