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)]
) 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
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.
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
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
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.
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 "metrebit", 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
Haha, you guys. Use it however you want. But trust me, if you put MiB
and GiB instead of the morecommon mb and gb [MB and GB] in your
applications, your users will probably have a harder time
understanding what you mean.
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 "metrebit", 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 "millibit" :)
which could be considered as the probability of a bit
.... this might be useful for quantum computing.
Avell Diroll <av*********@yahoo.frwrites:
I have to disagree: 'mb' should stand for "millibit" :)
Yes, you're right. My "metrebit" 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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by Mego 
last post: by

10 posts
views
Thread by Kristian Nybo 
last post: by

6 posts
views
Thread by Alexander Hunziker 
last post: by

9 posts
views
Thread by Dave 
last post: by

12 posts
views
Thread by PiotrKolodziej 
last post: by

6 posts
views
Thread by Wes 
last post: by

4 posts
views
Thread by cyberco 
last post: by

2 posts
views
Thread by levimc 
last post: by
           