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

前端 未结 15 1639
轮回少年
轮回少年 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:39

    Here is to convert bytes to kilo, mega, tera.

    #From bytes to kilo, mega, tera
    def  get_(size):
    
        #2**10 = 1024
        power = 2**10
        n = 1
        Dic_powerN = {1:'kilobytes', 2:'megabytes', 3:'gigabytes', 4:'Terabytes'}
    
        if size <= power**2 :
            size /=  power
            return size, Dic_powerN[n]
    
        else: 
            while size   >  power :
                n  += 1
                size /=  power**n
    
            return size, Dic_powerN[n]
    

提交回复
热议问题