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 7058
On Jun 12, 8:47 pm, samuraisam <samuraib...@gm ail.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*********@gm ail.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...@gm ail.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...@gm ail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gm ail.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...@gm ail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gm ail.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*********@gm ail.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.or g/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']
--
\ Contentsofsigna turemaysettledu ringshipping. |
`\ |
_o__) |
Ben Finney
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.
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.
Avell Diroll <av*********@ya hoo.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mego |
last post by:
Sto usando vb6 per creare un file .cub a a partire da una query:
LOCATION= d:\Test.cub;
SOURCE_DSN = "Provider=MSOLAP.2;Data Source=data-center;Initial
Catalog=FoodMart 2000";
CREATECUBE=CREATE CUBE LocalCube (
DIMENSION
,LEVEL OPTIONS(SORTBYNAME)
,LEVEL OPTIONS(SORTBYNAME)
,LEVEL OPTIONS(SORTBYNAME)
|
by: Kristian Nybo |
last post by:
Hi,
I'm writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes...
|
by: Alexander Hunziker |
last post by:
Hello group,
I have a program that reads data from a binary file. I know that at some
known position in the file there are 12 4 bytes long floating point
numbers. Here's how I read them now:
float temptriangle;
fread(&temptriangle, 4, 12, fp);
|
by: Dave |
last post by:
I am trying to call VerQueryValue from a C# program. VerQueryValue takes as
one of its parameters a pointer to a pointer to an array of bytes, which it
uses to return a pointer to the required array. Now, I can call it like this:
byte* lpBuffer;
int length;
string subBlock = @"\StringFileInfo\" + langCodePage +
@"\FileDescription";
result = VerQueryValue(buffer, subBlock, &lpBuffer, &length);
but then I don't have a byte to pass to an...
|
by: PiotrKolodziej |
last post by:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Thanks
PK
| |
by: Wes |
last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++.
The idea of the program is to open one file as input, read bytes from
it, do some bitwise operations on the bytes, and then write them to
this second file. However, when the second file is 15360 bytes long,
the program dies with a "Segmentation Fault (core dumped)" error!
I checked with gdb, and it says the last function to run was memcpy()
from libc, which would...
|
by: cyberco |
last post by:
I'm using web.py to send an image to the client. This works
(shortened):
print open(path, "rb").read()
but this doesn't:
img = Image.open(path)
img.thumbnail((10,10))
print img.getdata()
|
by: levimc |
last post by:
I know that that topic may be old to you but I looked at other more-
than-two-year-old topics
related to mine. However, I didn't find them working for my project
at all because its errors return back to me everytime.
The error I have on that project said:
"An unhandled exception of type 'System.EntryPointNotFoundException'
occurred in TestSysaudit.exe
|
by: RYKLOU |
last post by:
I am kinda new to php, but i do know what i am doing kinda, but i came across this error when i am trying to upload a file to my website.
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3714000 bytes) in /opt/lampp/htdocs/tutorials/php-mysql-tutorial/admin/image-gallery/library/functions.php on line 104
Platform:
Ubuntu 8.04 LST (where i make my programs, and test them before i upload them), Using XAMPP for...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |