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 360765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:31:12+00:00 2026-05-12T12:31:12+00:00

Before I re-invent this particular wheel, has anybody got a nice routine for calculating

  • 0

Before I re-invent this particular wheel, has anybody got a nice routine for calculating the size of a directory using Python? It would be very nice if the routine would format the size nicely in Mb/Gb etc.

  • 1 1 Answer
  • 1 View
  • 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-12T12:31:13+00:00Added an answer on May 12, 2026 at 12:31 pm

    This walks all sub-directories; summing file sizes:

    import os
    
    def get_size(start_path = '.'):
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                # skip if it is symbolic link
                if not os.path.islink(fp):
                    total_size += os.path.getsize(fp)
    
        return total_size
    
    print(get_size(), 'bytes')
    

    And a oneliner for fun using os.listdir (Does not include sub-directories):

    import os
    sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))
    

    Reference:

    • os.path.getsize – Gives the size in bytes
    • os.walk
    • os.path.islink

    Updated
    To use os.path.getsize, this is clearer than using the os.stat().st_size method.

    Thanks to ghostdog74 for pointing this out!

    os.stat – st_size Gives the size in bytes. Can also be used to get file size and other file related information.

    import os
    
    nbytes = sum(d.stat().st_size for d in os.scandir('.') if d.is_file())
    

    Update 2018

    If you use Python 3.4 or previous then you may consider using the more efficient walk method provided by the third-party scandir package. In Python 3.5 and later, this package has been incorporated into the standard library and os.walk has received the corresponding increase in performance.

    Update 2019

    Recently I’ve been using pathlib more and more, here’s a pathlib solution:

    from pathlib import Path
    
    root_directory = Path('.')
    sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question has been asked before, but I'm at my wit's end
Now, I could go ahead and write this using FileSystemWatcher etc, but before I
before I start I want to point out that I tagged this question as
Before, I was using VS 2008 command prompt and when I do clrver I
I will have a client application using a proxy to a WCF Service. This
I can't believe that this hasn't been asked before but as I am unable
I have already had this problem and solved it before with stackoverflows help but
First off, let me start by saying, I know this exact question has been
i'm using the code private void sendSms(String phoneNo, String message){ PendingIntent pi = PendingIntent.getActivity(this,
I'm developing and app where the user must login before using it. I customized

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.