Created
January 8, 2019 07:35
-
-
Save vernomcrp/40f9fe8eeadbfb1ed8774035f41114e0 to your computer and use it in GitHub Desktop.
Convert raw bytes amount to human readable information
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 math | |
| def bytes2human(n): | |
| # Credits to /u/cyberspacecowboy on reddit¬ | |
| # https://www.reddit.com/r/Python/comments/5xukpd/-/dem5k12/¬ | |
| symbols = (' B', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', | |
| ' YiB') | |
| i = math.floor(math.log(abs(n)+1, 2) / 10) | |
| return '%.1f%s' % (n/2**(i*10), symbols[i]) | |
| if __name__ == '__main__': | |
| from sys import stdin, stdout | |
| stdout.write( | |
| bytes2human( | |
| stdin.readline().strip() | |
| ) | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits to
reddit /u/cyberspacecowboyandRay Chen