Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:47:55+00:00 2026-05-20T10:47:55+00:00

I am using a library that reads a file and returns its size in

  • 0

I am using a library that reads a file and returns its size in bytes.

This file size is then displayed to the end user; to make it easier for them to understand it, I am explicitly converting the file size to MB by dividing it by 1024.0 * 1024.0. Of course this works, but I am wondering is there a better way to do this in Python?

By better, I mean perhaps a stdlib function that can manipulate sizes according to the type I want. Like if I specify MB, it automatically divides it by 1024.0 * 1024.0. Somethign on these lines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:47:56+00:00Added an answer on May 20, 2026 at 10:47 am

    There is hurry.filesize that will take the size in bytes and make a nice string out if it.

    >>> from hurry.filesize import size
    >>> size(11000)
    '10K'
    >>> size(198283722)
    '189M'
    

    Or if you want 1K == 1000 (which is what most users assume):

    >>> from hurry.filesize import size, si
    >>> size(11000, system=si)
    '11K'
    >>> size(198283722, system=si)
    '198M'
    

    It has IEC support as well (but that wasn’t documented):

    >>> from hurry.filesize import size, iec
    >>> size(11000, system=iec)
    '10Ki'
    >>> size(198283722, system=iec)
    '189Mi'
    

    Because it’s written by the Awesome Martijn Faassen, the code is small, clear and extensible. Writing your own systems is dead easy.

    Here is one:

    mysystem = [
        (1024 ** 5, ' Megamanys'),
        (1024 ** 4, ' Lotses'),
        (1024 ** 3, ' Tons'), 
        (1024 ** 2, ' Heaps'), 
        (1024 ** 1, ' Bunches'),
        (1024 ** 0, ' Thingies'),
        ]
    

    Used like so:

    >>> from hurry.filesize import size
    >>> size(11000, system=mysystem)
    '10 Bunches'
    >>> size(198283722, system=mysystem)
    '189 Heaps'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a library that has headers without the .h This defeats visual
Using the D3DX library that is a part of directX, specifically directx9 in this
I'm using a third-party library that returns XML that is not valid, because it
I am using a charting javascript library that expects its data in a specific
I have a library that handles reading and writing a cache file. This library
Alright, I have library I wrote in C that reads a file and provides
I am using a library that prints a bunch of superfluous information to the
I'm using a library that defines an interface: template<class desttype> void connect(desttype* pclass, void
In C#, I am using a library that defines an enum. I would like
I need some help on compiler flags in c++. I'm using a library that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.