Skip to content

Instantly share code, notes, and snippets.

@JPFrancoia
Last active August 3, 2017 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPFrancoia/d0b4341c36e1db06a17d49601eae9875 to your computer and use it in GitHub Desktop.
Save JPFrancoia/d0b4341c36e1db06a17d49601eae9875 to your computer and use it in GitHub Desktop.
Sizeof
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