Portál AbcLinuxu, 12. května 2025 03:07
def getdirsize(base): size = 0 for root, dirs, files in os.walk(base): for file in files: filepath = os.path.join(root, file) if os.path.islink(filepath): continue size = size + os.path.getsize(filepath) return sizeAle je to jen jednoduchá varianta, která za adresáře a linky nic nepřipočítává.
def human_readable(x, suffix = '', binary = True): prefixes_dec = ['', 'k', 'M', 'G', 'T', 'P'] prefixes_bi = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi'] if binary: base = 1024 prefixes = prefixes_bi else: base = 1000 prefixes = prefixes_dec mul = base/999.5 mag = int(math.log(x * mul, base)) prefix = prefixes[mag] not_rounded = float(x) / base**mag r_order = 10**(2 - int(math.log(not_rounded, 10))) rounded = math.ceil(not_rounded * r_order - 0.5) / r_order return '%g %s%s' %(rounded, prefix, suffix)Parametrem "binary" (který je True nebo False) rozlišuje decimální a binární předpony. Ty jsou sice na můj vkus trochu nezvyklé, ale co se dá dělat - viz: http://physics.nist.gov/cuu/Units/binary.html
print human_readable(648218, 'Hz', binary=False) print human_readable(187, 'bit/s', binary=True)A teď mi někdo řekněte, jak by měla vypadat "human readable" 1001 bitů. Je to už čtyřmístné, ale když se to převede na Ki bity, tak se to scvrkne na 0.98. Blbé, co?
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.