How to calculate a directory size using Python?

A directory is simply defined as a collection of subdirectories and files. These subdirectories are separated using a "/" operator in a directory hierarchy. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory also known as the "root" directory.

Calculating the total size of a directory, including all its files and subdirectories, is a common task in Python, especially when dealing with disk usage monitoring, backup management, or cleaning up storage. Python provides multiple ways to accomplish this efficiently using built-in modules such as os and pathlib.

In this article, we will explore different approaches to find the size of a directory in Python:

  • Using os.path.getsize() method
  • Using os.stat().st_size property
  • Using du command in *NIX OSes

Using os.path.getsize() Method

The os.path.getsize() method retrieves the size of a single file within a directory. To get the total directory size, we add up sizes of all files present in it. To walk through all files in the directory, we use the os.walk() method in combination with this method.

This method accepts the file path as its argument and returns the size of a file in bytes.

Example with os.walk()

Here's how to calculate the size of a directory using os.path.getsize() with os.walk() ?

import os

def get_directory_size(start_path):
    total_size = 0
    for path, dirs, files in os.walk(start_path):
        for f in files:
            fp = os.path.join(path, f)
            total_size += os.path.getsize(fp)
    return total_size

# Get size of current directory
directory_size = get_directory_size('.')
print(f"Directory size: {directory_size} bytes")
Directory size: 268008 bytes

Example with os.scandir()

Here we use the scandir() method to scan the current directory and get only file sizes (not subdirectories) ?

import os

total_size = 0
start_path = '.'  # Current directory

with os.scandir(start_path) as entries:
    for entry in entries:
        if entry.is_file():
            total_size += os.path.getsize(entry.path)
            
print(f"Directory size: {total_size} bytes")
Directory size: 189783 bytes

Example with os.listdir()

Using os.listdir() method to get directory contents and calculate total size ?

import os

total_size = 0
start_path = '.'  # Current directory

for filename in os.listdir(start_path):
    file_path = os.path.join(start_path, filename)
    if os.path.isfile(file_path):
        total_size += os.path.getsize(file_path)
        
print(f"Directory size: {total_size} bytes")
Directory size: 193896 bytes

Using os.stat().st_size Property

Another way to retrieve file size is using the os.stat().st_size property. The os.stat() method returns file statistics, and we use the st_size attribute to get the size in bytes.

Example with pathlib

Using pathlib to list files and calculate their sizes with stat().st_size ?

from pathlib import Path

root_directory = Path('.')
total_size = 0

for file_path in root_directory.glob("*"):
    if file_path.is_file():
        file_size = file_path.stat().st_size
        total_size += file_size
        
print(f"Size of current directory: {total_size} bytes")
Size of current directory: 189786 bytes

Recursive Directory Size Calculation

Here we create a recursive function to calculate the size of a directory including all subdirectories ?

import os

def get_dir_size_recursive(path):
    total = 0
    with os.scandir(path) as entries:
        for entry in entries:
            if entry.is_file():
                total += entry.stat().st_size
            elif entry.is_dir():
                total += get_dir_size_recursive(entry.path)
    return total

size = get_dir_size_recursive('.')
print(f"Total directory size: {size} bytes")
Total directory size: 268157 bytes

Using du Command in *NIX OS

If you're on a *NIX operating system (Linux, macOS, Unix), you can use the du command through Python's subprocess module for a simpler approach.

Example

Using the du command to get human-readable directory size ?

import subprocess

path = '.'
try:
    result = subprocess.check_output(['du', '-sh', path])
    size = result.split()[0].decode('utf-8')
    print(f"Directory size: {size}")
except subprocess.CalledProcessError:
    print("Error: du command failed or not available")
Directory size: 8.0K

Comparison of Methods

Method Recursive Platform Output Format
os.walk() + getsize() Yes Cross-platform Bytes
os.scandir() + stat() Manual Cross-platform Bytes
pathlib + glob() No Cross-platform Bytes
subprocess + du Yes *NIX only Human-readable

Conclusion

Use os.walk() with os.path.getsize() for comprehensive recursive directory size calculation. For *NIX systems, the du command provides the simplest solution with human-readable output. Choose the method based on your platform requirements and whether you need recursive traversal.

Updated on: 2026-03-24T18:24:32+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements