def bytes_to_string(bytes):
    """Converts number of bytes to a string.  Based on old code here:
        http://dev.jmoiron.net/hg/weechat-plugins/file/tip/mp3.py

    Uses proc-like units (capital B, lowercase prefix).  This only takes a
    few microseconds even for numbers in the terabytes.
    """
    units = ['B', 'kB', 'mB', 'gB', 'tB']
    negate = bytes < 0
    if negate:
        bytes = -bytes
    factor = 0
    while bytes / (1024.0 ** (factor + 1)) >= 1:
        factor += 1
    return '%s%0.1f %s' % \
            ('-' if negate else '', bytes / (1024.0 ** factor), units[factor])