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

  • Home
  • SEARCH
  • 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 6375295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:35:49+00:00 2026-05-25T01:35:49+00:00

I’d like to create python command line code that is able to print directory

  • 0

I’d like to create python command line code that is able to print directory tree with sizes of all subdirectories (from certain directory) and most frequent extensions… I will show the example output.

  • root_dir (5 GB, jpg (65 %): avi ( 30 %) : pdf (5 %))

— aa (3 GB, jpg (100 %) )

— bb (2 GB, avi (20 %) : pdf (2 %) )

— bbb (1 GB, …)

— bb2 (1 GB, …)

— cc (1 GB, pdf (100 %) )

The format is :

nesting level, directory name (size of the directory with all files and subdirectories, most frequent extensions with size percentages in this directory.

I have this code snippet so far. The problem is that it counts only file sizes in directory, so the resulting size is smaller than real size of the directory. Other problem is how to put it all together to print the tree I defined above without redundant computations.

  • 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-25T01:35:49+00:00Added an answer on May 25, 2026 at 1:35 am

    Calculating directory sizes really isn’t python’s strong suit, as explained in this post: very quickly getting total size of folder. If you have access to du and find, by all means use that. You can easily display the size of each directory with the following line:

    find . -type d -exec du -hs "{}" \;
    

    If you insist in doing this in python, you may prefer post-order traversal over os.walk, as suggested by PableG. But using os.walk can be visually cleaner, if efficiency is not the utmost factor for you:

    import os, sys
    from collections import defaultdict
    
    def walkIt(folder):
        for (path, dirs, files) in os.walk(folder):
            size = getDirSize(path)
            stats = getExtensionStats(files)
    
            # only get the top 3 extensions
            print '%s (%s, %s)'%(path, size, stats[:3])
    
    def getExtensionStats(files):
        # get all file extensions
        extensions = [f.rsplit(os.extsep, 1)[-1] 
            for f in files if len(f.rsplit(os.extsep, 1)) > 1]
    
        # count the extensions
        exCounter = defaultdict(int)
        for e in extensions:
            exCounter[e] += 1
    
        # convert count to percentage
        percentPairs = [(e, 100*ct/len(extensions)) for e, ct in exCounter.items()]
    
        # sort them
        percentPairs.sort(key=lambda i: i[1])
        return percentPairs
    
    def getDirSize(root):
        size = 0
        for path, dirs, files in os.walk(root):
            for f in files:
                size +=  os.path.getsize( os.path.join( path, f ) )
        return size
    
    if __name__ == '__main__':
        path = sys.argv[1] if len(sys.argv) > 1 else '.'
        walkIt(path)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.