Created
August 30, 2017 13:48
-
-
Save leeavital/21b0f94d06f78a90e42f45398276fc8d to your computer and use it in GitHub Desktop.
convert a byte count to a human readable form
This file contains hidden or 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
#!/usr/bin/env python3 | |
import sys | |
import math | |
totalBytes = int(sys.argv[1]) | |
units = ["bytes", "kilobytes", "megabytes", "gigabytes", "terrabytes", "petabytes"] | |
unitIndex = int(math.log(totalBytes, 1024)) | |
bytesPerUnit = math.pow(1024, unitIndex) | |
print("%.2f %s" % (totalBytes / (bytesPerUnit + 0.0), units[unitIndex])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment