Last active
August 3, 2017 14:50
-
-
Save JPFrancoia/d0b4341c36e1db06a17d49601eae9875 to your computer and use it in GitHub Desktop.
Sizeof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sizeof(nbr, unit="o", rounded=1): | |
"""On récupère la taille en octets, et on la formate en Mo | |
ou en Go si plus grand que 1000 Mo. Si on passe l'argument 'b' | |
en paramètre, on calcule en ibytes""" | |
# http://www.fevrierdorian.com/blog/post/2011/06/26/Taille-d-un-fichier-humainement-comprehensible-en-Python | |
if unit == "o": | |
if nbr < 1000.0: | |
return "{0} {1}".format(round(nbr, rounded), 'octets') | |
for x in ['ko', 'Mo', 'Go', 'To']: | |
if nbr < 1000.0: | |
return "{0} {1}".format(round(nbr, rounded), x) | |
else: | |
nbr /= 1000.0 | |
elif unit == "b": | |
if nbr < 1024.0: | |
return "{0} {1}".format(round(nbr, rounded), 'bytes') | |
for x in ['KiB', 'MiB', 'GiB', 'TiB']: | |
if nbr < 1024.0: | |
return "{0} {1}".format(round(nbr, rounded), x) | |
else: | |
nbr /= 1024.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment