Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
bytes to to mb, gb, etc in python
def bytesto(bytes, to, bsize=1024):
"""convert bytes to megabytes, etc.
sample code:
print('mb= ' + str(bytesto(314575262000000, 'm')))
sample output:
mb= 300002347.946
"""
a = {'k' : 1, 'm': 2, 'g' : 3, 't' : 4, 'p' : 5, 'e' : 6 }
r = float(bytes)
for i in range(a[to]):
r = r / bsize
return(r)
@sunnyjiechao

This comment has been minimized.

Copy link

sunnyjiechao commented Mar 2, 2018

when i use the function of sys.getsizeof() , how to Convert into mb, gb, etc .
thanks

@navercm418

This comment has been minimized.

Copy link

navercm418 commented Mar 13, 2020

Nice job. i use this along with psutil for sys admin reports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.