Skip to content

Instantly share code, notes, and snippets.

@vernomcrp
Created January 8, 2019 07:35
Show Gist options
  • Select an option

  • Save vernomcrp/40f9fe8eeadbfb1ed8774035f41114e0 to your computer and use it in GitHub Desktop.

Select an option

Save vernomcrp/40f9fe8eeadbfb1ed8774035f41114e0 to your computer and use it in GitHub Desktop.
Convert raw bytes amount to human readable information
#!/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()
)
)
@vernomcrp
Copy link
Copy Markdown
Author

Credits to reddit /u/cyberspacecowboy and Ray Chen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment