Skip to content

Instantly share code, notes, and snippets.

@leeavital
Created August 30, 2017 13:48
Show Gist options
  • Save leeavital/21b0f94d06f78a90e42f45398276fc8d to your computer and use it in GitHub Desktop.
Save leeavital/21b0f94d06f78a90e42f45398276fc8d to your computer and use it in GitHub Desktop.
convert a byte count to a human readable form
#!/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