Skip to content

Instantly share code, notes, and snippets.

@Ricky-Wilson
Created December 4, 2017 10:10
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 Ricky-Wilson/0d608bb7a3ed0cfa5318b444bad594a9 to your computer and use it in GitHub Desktop.
Save Ricky-Wilson/0d608bb7a3ed0cfa5318b444bad594a9 to your computer and use it in GitHub Desktop.
Convert file sizes to human readable format
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
power = math.pow(1024, i)
size = round(size_bytes / power, 2)
return "%s %s" % (size, size_name[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment