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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:19:19+00:00 2026-06-15T08:19:19+00:00

I am trying to sort the files based on their size and store the

  • 0

I am trying to sort the files based on their size and store the log in a file. But I get an error that says ‘getsize’ not defined. Please help me fix this.

from ClientConfig import ClientConfig
import os
import os.path

class VerifyFileNSize:
    def __init__(self):
        self.config = ClientConfig()
        self.parseInfo()

    def parseInfo(self):
        count = 0
        size = 0
        sort_file = []
        originalPath = os.getcwd()
        os.chdir(self.config.Root_Directory_Path())    
        log = open(self.config.File_Count(),'wb')        
        for root, dirs, files in os.walk("."):            
            for f in files:
                sort_file.append(os.path.join(root, f))

        sorted_file = sorted(sort_file, key=getsize)

        for f in sorted_file:
            log.write((str(os.path.getsize(f)) + " Bytes" + "|" + f  +         os.linesep).encode())                
            size += os.path.getsize(f)
            count += 1
        totalPrint = ("Client: Root:" + self.config.Root_Directory_Path() + " Total     Files:" + str(count) + " Total Size in Bytes:" + str(size) + " Total Size in     MB:" + str(round(size /1024/1024, 2))).encode()
        print(totalPrint.decode())
        log.write(totalPrint)
        log.close()
        os.chdir(originalPath)

if __name__ == "__main__":
    VerifyFileNSize()
  • 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-15T08:19:20+00:00Added an answer on June 15, 2026 at 8:19 am

    getsize is not defined in the name space where sorted is called. It’s a function in module os.path, which you imported, so you can refer to it like this:

    sorted_file = sorted(sort_file, key=os.path.getsize)
    

    Another possibility is to do:

    from os.path import join, getsize
    

    or even:

    from os.path import *
    

    that will allow you to do:

    sorted_file = sorted(sort_file, key=getsize)
    

    But the last option is not really recommended, you should try to import only names that you really need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read and sort a csv file that has data that
I am trying to read the latest 10 files based on their creation date
I am trying to sort a file based on the users choice of 2
I'm trying to create ranges of IP addresses based off a file containing a
I'm trying to sort through several thousand scanned files and sort them into folders
I found some this promising code on activestate.com to sort huge files. I'm trying
I am trying to call a C function that is defined in another file.
I am trying to get the list of files sorted by modification date. I
I'm trying to sort through a collection of DeepZoom sub-images based on arbitrary data
I am trying to sort a table based on the first value from smallest

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.