Python format size application (converting B to KB, MB, GB, TB)

前端 未结 15 1643
轮回少年
轮回少年 2021-02-01 16:13

I am trying to write an application to convert bytes to kb to mb to gb to tb. Here\'s what I have so far:

def size_format(b):
    if b < 1000:
              r         


        
15条回答
  •  孤独总比滥情好
    2021-02-01 16:48

    good idea for me:

    def convert_bytes(num):
        """
        this function will convert bytes to MB.... GB... etc
        """
        step_unit = 1000.0 #1024 bad the size
    
        for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
            if num < step_unit:
                return "%3.1f %s" % (num, x)
            num /= step_unit
    

提交回复
热议问题