(number_of_bytes, precision=1)
return str(number_of_bytes) + ' ' + unit
流量bytes转换为kb, mb, gb等单位
流量bytes转换为kb, mb, gb等单位
流量bytes转换为kb mb gb等单位
def bytes_2_human_readable(number_of_bytes, precision=1):
    """
    流量bytes转换为kb, mb, gb等单位
    """
    if number_of_bytes < 0:
        raise ValueError("!!! number_of_bytes can't be smaller than 0 !!!")
 
    step_to_greater_unit = 1024.
 
    number_of_bytes = float(number_of_bytes)
    unit = 'bytes'
 
    if (number_of_bytes / step_to_greater_unit) >= 1:
        number_of_bytes /= step_to_greater_unit
        unit = 'KB'
 
    if (number_of_bytes / step_to_greater_unit) >= 1:
        number_of_bytes /= step_to_greater_unit
        unit = 'MB'
 
    if (number_of_bytes / step_to_greater_unit) >= 1:
        number_of_bytes /= step_to_greater_unit
        unit = 'GB'
 
    if (number_of_bytes / step_to_greater_unit) >= 1:
        number_of_bytes /= step_to_greater_unit
        unit = 'TB'
 
    number_of_bytes = round(number_of_bytes, precision)
 
    return str(number_of_bytes) + ' ' + unit
def bytes_2_human_readable ( number_of_bytes , precision = 1 ) : if number_of_bytes < 0 : raise ValueError ( "!!! number_of_bytes can't be smaller than 0 !!!" ) step_to_greater_unit = 1024. number_of_bytes = float ( number_of_bytes ) unit = 'bytes' if ( number_of_bytes / step_to_greater_unit ) >= 1 : number_of_bytes /= step_to_greater_unit unit = 'KB' if ( number_of_bytes / step_to_greater_unit ) >= 1 : number_of_bytes /= step_to_greater_unit unit = 'MB' if ( number_of_bytes / step_to_greater_unit ) >= 1 : number_of_bytes /= step_to_greater_unit unit = 'GB' if ( number_of_bytes / step_to_greater_unit ) >= 1 : number_of_bytes /= step_to_greater_unit unit = 'TB' number_of_bytes = round ( number_of_bytes , precision ) return str ( number_of_bytes ) + ' ' + unit