Last active
June 7, 2017 04:47
-
-
Save mortymacs/b4e582746d2667efe148500944a91eb4 to your computer and use it in GitHub Desktop.
Convert Size to Human Readable String
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 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