developerdong
6/12/2017 - 1:47 AM

智能转换Bytes为KB/MB/GB/TB/PB...

智能转换Bytes为KB/MB/GB/TB/PB...

# -*- coding: utf-8 -*-

"""智能转换 bytes 为 kb/mb/gb/tb/pb...
"""

import math

def convertBytes(bytes, lst=['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']):
    i = 0 if bytes == 0 else int(math.floor(math.log(bytes, 1024)))# 求对数向下取整(对数:若 a**b = N 则 b 叫做以 a 为底 N 的对数)
    if i >= len(lst):
        i = len(lst) - 1
    return ('%.2f' + " " + lst[i]) % (bytes/math.pow(1024, i))

def main():
    lst = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
    bytes = input('Bytes: ')
    print convertBytes(bytes, lst)

if __name__ == '__main__':
    main()