
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Directory Size Using Python
A directory is simply defined as a collection of subdirectories and single files or either one of them. 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 and also known as the "root" directory.
Calculating the total size of a directory, i.e., all its files and subdirectories which 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 is used to retrieve the size of a single file within a directory. To get the total directory size, we can add up sizes of all the files present in it. But, to walk through all the files in this directory, we use can os.walk() method in addition to this method.
This method accepts the file path as its argument and returns the size of a file in bytes ().
Example
Let us see an example to calculate the size of a local directory using the method os.path.getsize() -
import os total_size = 0 start_path = '.' # To get size of current directory 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) print("Directory size: " + str(total_size))
Following is the output of the above program -
Directory size: 268008
Instead of using the os.walk() method, we can also use the os.scandir() method or os.listdir() method to list the files and retrieve their individual sizes.
Example
In this example, we are using the scandir() method to scan the current directory and recursively get the sizes of all the files present in it. The sizes of all the files are added together to retrieve the total size of the directory -
import os total_size = 0 start_path = '.' # To get size of current directory with os.scandir(start_path) as d: for f in d: if f.is_file(): fp = os.path.join(start_path, f) total_size += os.path.getsize(fp) print("Directory size: " + str(total_size))
The output for the program above is as follows -
Directory size: 189783
Example
In this example, we are using the os.listdir() method instead of os.scandir() method to get the size of the directory -
import os total_size = 0 start_path = '.' # To get size of current directory for f in os.listdir(start_path): f = os.path.join(start_path, f) total_size += os.path.getsize(f) print("Directory size: " + str(total_size))
Here is the output of the above program ?
Directory size: 193896
Using os.stat().st_size Property
Another way to retrieve the size of a file is by using os.stat().st_size property. The os.stat() method is used to get the size, i.e., in bytes, or other file-related information. Since we just need the information about the size of a file, we are using the st_size property only.
Example
Following is an example, in which we are using the glob() method to list all the files present in the current directory, and then their sizes are calculated recursively using the os.stat().st_size property -
from pathlib import Path root_directory = Path('.') size = 0 for f in root_directory.glob("*"): if f.is_file(): sm = f.stat().st_size size = sm + size print("Size of current directory:", size)
When we execute the above program then the result will be as follows -
Size of current directory: 189786
Example
Here in this example, we will use os.scandir() method to list all the files in the directory instead of the glob() method -
import os def get_dir_size(path): total = 0 with os.scandir(path) as d: for f in d: if f.is_file(): total += f.stat().st_size elif f.is_dir(): total += get_dir_size(f.path) return total print("The size of current directory", get_dir_size('.'))
The output for the above-given program is displayed as follows -
The size of current directory 268157
In " *NIX " OS
If we're on *NIX OS, then we could simply call the du command using the subprocess module, as it is much easier than the way above.
Example
The size of the current directory in a *NIX OS can be simply calculated as shown in the example below -
import subprocess path = '.' size = subprocess.check_output(['du','-sh', path]).split()[0].decode('utf-8') print("Directory size: " + size)
The size of the current directory will be returned as follows. However, the output differs for different directories.
Directory size: 8.0K