Sort Folders by Size in Python

In this tutorial, we're going to learn how to sort folders by size.

First of all, let's see the folders that we'll be sorting.

/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder:

sub-folders

As you can see, the RootFolder folder contains "10" sub-folders of different sizes.

Now, let's get started.

Sort folders by size

First, we need to write a function that returns the size of a given folder.

Code:


import os

def dir_size(path):
    #initialize the size
    total_size = 0
    
    #use the walk() method to navigate through directory tree
    for dirpath, dirnames, filenames in os.walk(path):
        for i in filenames:
            
            #use join to concatenate all the components of path
            f = os.path.join(dirpath, i)
            
            #use getsize to generate size in bytes and add it to the total size
            total_size += os.path.getsize(f)
    return total_size


Let's test our function.


print(dir_size("/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub5"))

Output:

2813

Now, to sort folders by size, we need to follow these steps:

  1. Getting all files in the RootFolder folder using the os.listdir() method.
  2. Checking each file if is a folder using the isdir() method.
  3. Append folder's name, folder's path folder's size to a list.
  4. Sorting the list by size using the sort() method.

# base folder
base_dir = "/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder"
# Get all sub files
files = os.listdir(base_dir)

folders = []

for e in files:
    #join base folder's name with sub file
    filepath = os.path.join(base_dir, e)
    #check if the file is directory(folder)
    if os.path.isdir(filepath):
        #append the folder_name, folder_path & size to the folders list
        folders.append({"name":e, "path":filepath, "size":dir_size(filepath)})


#sort sub_folders by size
# reverse = True: returns largest to smallest
# reverse = False: returns smallest to largest
folders.sort(key=lambda filename: filename['size'], reverse=True)

Notice:

reverse = True: returns largest to smallest

reverse = False: returns smallest to largest

Now, let's show the result after sorting.


for f in folders:
    print("#"*50)
    #print folder's name
    print("folder name: " + f['name'])
    #print folder's path
    print("folder path: " + f['path'])
    #print folder's size
    print("folder size: " + str(f['size']))


Output:


##################################################
file name: sub3
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub3
file size: 3404128
##################################################
file name: sub10
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub10
file size: 1761395
##################################################
file name: sub4
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub4
file size: 512059
##################################################
file name: sub8
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub8
file size: 106092
##################################################
file name: sub6
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub6
file size: 48131
##################################################
file name: sub2
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub2
file size: 10434
##################################################
file name: sub9
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub9
file size: 6730
##################################################
file name: sub5
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub5
file size: 2813
##################################################
file name: sub1
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub1
file size: 549
##################################################
file name: sub7
file filepath: /home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub7
file size: 0

Write the sort_by_size method

After explaining how to sort folders by size, now let's re-write a method that you can use in your app:


class SortFolders():

    def __init__(self, dir_path, reverse=True):
        self.dir_path = dir_path
        self.reverse = reverse


    def dir_size(self, path):
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(path):
            for i in filenames:
                f = os.path.join(dirpath, i)
                total_size += os.path.getsize(f)

        return total_size


    @property
    def sort_by_size(self):
        files = os.listdir(self.dir_path)
        folders = []
        for e in files:
            filepath = os.path.join(self.dir_path, e)
            if os.path.isdir(filepath):
                folders.append({"filename":e, "filepath":filepath, "size":self.dir_size(filepath)})

        folders.sort(key=lambda filename: filename['size'], reverse=self.reverse)

        return folders

How to use it?


SortFolders(dir_path="base dir", reverse=False).sort_by_size

  • dir_path: required
  • reverse: optional (reverse=True by default)

Example


print(SortFolders(dir_path="/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder", reverse=False).sort_by_size)

Output


[{'filename': 'sub7', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub7', 'size': 0}, {'filename': 'sub1', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub1', 'size': 549}, {'filename': 'sub5', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub5', 'size': 2813}, {'filename': 'sub9', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub9', 'size': 6730}, {'filename': 'sub2', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub2', 'size': 10434}, {'filename': 'sub6', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub6', 'size': 48131}, {'filename': 'sub8', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub8', 'size': 106092}, {'filename': 'sub4', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub4', 'size': 512059}, {'filename': 'sub10', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub10', 'size': 1761395}, {'filename': 'sub3', 'filepath': '/home/pytutorial/Desktop/pytutorial/tutorials/RootFolder/sub3', 'size': 3404128}]