471,357 Members | 1,210 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Bytes/File Size Format Function

Quick file size formatting for all those seekers out there...

import math

def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)

Jun 13 '07 #1
9 6905
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...

import math

def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Life is odd. I just came to the group with the specific purpose of
asking this exact question. Who are you? :)

Thanks!

~Sean

Jun 13 '07 #2
samuraisam <sa*********@gmail.comwrote:
>
Quick file size formatting for all those seekers out there...

import math

def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
I have a couple of picky comments. The abbreviation for "bytes" should be
"B"; small "b" is bits by convention. All of the prefixes above "k" should
be capitalized: kB, MB and GB, not mb and gb.

The truly anal retentive would probably argue that you should be using kiB,
MiB, and GiB, since you are basing your scale on 1024 instead of 1000.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 13 '07 #3
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...

import math

def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...

~Sean

Jun 13 '07 #4
On Jun 13, 12:48 am, half.ital...@gmail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)

Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...

~Sean

Jun 13 '07 #5
On Jun 13, 12:48 am, half.ital...@gmail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)

Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...

~Sean
I take it back.

Jun 13 '07 #6
samuraisam <sa*********@gmail.comwrites:
Quick file size formatting for all those seekers out there...

import math

def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
The output doesn't match the calculation.

The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
units. And so on. The SI units have definitions that are only muddied
by misusing them this way.

Especially since we now have units that actually do mean what we want
them to. The units that match the calculation you're using are 'KiB',
'MiB', 'GiB' and so on.

<URL:http://en.wikipedia.org/wiki/Binary_prefix>

Dividing by 1024 doesn't give 'kb', it gives 'KiB'. Likewise for the
rest of the units. So the list of units should be::

['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']

--
\ Contentsofsignaturemaysettleduringshipping. |
`\ |
_o__) |
Ben Finney
Jun 13 '07 #7
Haha, you guys. Use it however you want. But trust me, if you put MiB
and GiB instead of the more-common mb and gb [MB and GB] in your
applications, your users will probably have a harder time
understanding what you mean.

Jun 13 '07 #8
Ben Finney wrote:
The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
units. And so on. The SI units have definitions that are only muddied
by misusing them this way.
I have to disagree: 'mb' should stand for "milli-bit" :)
which could be considered as the probability of a bit
.... this might be useful for quantum computing.

Jun 13 '07 #9
Avell Diroll <av*********@yahoo.frwrites:
I have to disagree: 'mb' should stand for "milli-bit" :)
Yes, you're right. My "metre-bit" was wrong.

--
\ "Whenever you read a good book, it's like the author is right |
`\ there, in the room talking to you, which is why I don't like to |
_o__) read good books." -- Jack Handey |
Ben Finney
Jun 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Alexander Hunziker | last post: by
12 posts views Thread by PiotrKolodziej | last post: by
4 posts views Thread by cyberco | last post: by
2 posts views Thread by levimc | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.