Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active June 7, 2017 04:47
Show Gist options
  • Select an option

  • Save mortymacs/b4e582746d2667efe148500944a91eb4 to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/b4e582746d2667efe148500944a91eb4 to your computer and use it in GitHub Desktop.
Convert Size to Human Readable String
#!/usr/bin/env python
import sys
types = ["KB","MB","GB","TB","PB","EB","ZB","YB"]
def convert(s):
div = 1024
i = 0
while i < len(types):
s /= div
if s < div:
print("{:5.3f} {}".format(s, types[i]))
return
i += 1
if __name__ == "__main__":
if (len(sys.argv) != 2) or (len(sys.argv) == 2 and sys.argv[1].isdigit() is False):
print("Invalid input")
else:
convert(int(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment