Created
November 13, 2023 23:50
-
-
Save christinebuckler/d91d2bc4a943077fc396e1ce19fc4138 to your computer and use it in GitHub Desktop.
convert bytes to higher file size amounts
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
import math | |
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))) | |
p = math.pow(1024, i) | |
s = round(size_bytes / p, 2) | |
return "%s %s" % (s, size_name[i]) | |
# https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment