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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:15:32+00:00 2026-06-11T13:15:32+00:00

Lets say my structure is like this /– am here /one/some/dir /two /three/has/many/leaves /hello/world

  • 0

Lets say my structure is like this

/-- am here
/one/some/dir
/two
/three/has/many/leaves
/hello/world

and say /one/some/dir contains a big file, 500mb, and /three/has/many/leaves contains a 400mb file in each folder.

I want to generate the size for each directory, to have this output

/ - in total for all
/one/some/dir 500mb
/two 0 
/three/has/many/leaved - 400mb
/three/has/many 800
/three/has/ 800+someotherbigfilehere

How would I go about this?

  • 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-06-11T13:15:33+00:00Added an answer on June 11, 2026 at 1:15 pm

    Have a look at os.walk. Specifically, the documentation has an example to find the size of a directory:

    import os
    from os.path import join, getsize
    for root, dirs, files in os.walk('python/Lib/email'):
        print root, "consumes",
        print sum(getsize(join(root, name)) for name in files),
        print "bytes in", len(files), "non-directory files"
        if 'CVS' in dirs:
            dirs.remove('CVS')  # don't visit CVS directories
    

    This should be easy enough to modify for your purposes.


    Here’s an untested version in response to your comment:

    import os
    from os.path import join, getsize
    dirs_dict = {}
    
    #We need to walk the tree from the bottom up so that a directory can have easy
    # access to the size of its subdirectories.
    for root, dirs, files in os.walk('python/Lib/email',topdown = False):
    
        # Loop through every non directory file in this directory and sum their sizes
        size = sum(getsize(join(root, name)) for name in files) 
    
        # Look at all of the subdirectories and add up their sizes from the `dirs_dict`
        subdir_size = sum(dirs_dict[join(root,d)] for d in dirs)
    
        # store the size of this directory (plus subdirectories) in a dict so we 
        # can access it later
        my_size = dirs_dict[root] = size + subdir_size
    
        print '%s: %d'%(root,my_size) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array with a structure like this: $arr= Array( array(
Lets say that I have a database structure with three tables that look like
Lets say I have a structure like this: Object A contains an instance of
Lets say I have an xml structure like this: <s> <a> <b id=myid>value1</b> <c
let's say I have a html structure like this -> (note: in my code
Let's say I have a first structure like this: typedef struct { int ivalue;
So, lets say that we have this structure: <div id='allTempLinkHolder'> <div class='tempLink'>Value</div> <div class='tempLink'>Value
I have an XML doc with a structure like this: <Book> <Title title=Door Three/>
Lets say you would like to add items to a data structure in C#
Hi I am trying to use fortran structure like this type some u !

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.