Python Folder Size Calculator Example
Build a simple Python script that calculates the total size of files inside a folder. This example helps beginners practice working with file paths, loops, and the os module.
If you want the shortest working version first, start here:
import os
folder_path = "."
total_size = 0
for name in os.listdir(folder_path):
path = os.path.join(folder_path, name)
if os.path.isfile(path):
total_size += os.path.getsize(path)
print(f"Total size: {total_size} bytes")
This quick version adds the size of files in one folder only. A later section expands it to include subfolders.
What this example does
This example shows how to:
- Calculate the total size of files in a folder
- Combine
os.listdir(),os.path.join(), andos.path.getsize() - Understand how file system paths work in Python
- Solve one practical task with a small script
This is a good beginner project because it uses a loop, file paths, and built-in standard library tools in one place.
What you need to know first
Before reading the code, it helps to understand a few basics:
- A folder can contain files and subfolders
- A file path tells Python where a file or folder is
- The
osmodule gives you tools for working with files and folders - A loop lets you process items one by one
If file paths still feel confusing, see working with file paths in Python.
Simple version: total size of files in one folder
This version checks one folder only. It does not go inside subfolders.
import os
folder_path = "."
total_size = 0
for name in os.listdir(folder_path):
path = os.path.join(folder_path, name)
if os.path.isfile(path):
total_size += os.path.getsize(path)
print(f"Total size: {total_size} bytes")
How it works
os.listdir(folder_path)gets the names of items in the folderos.path.join(folder_path, name)builds the full pathos.path.isfile(path)checks whether the item is a fileos.path.getsize(path)gets the file size in bytes
Why os.path.join() matters
Do not build paths by hand with string addition. This can break on different operating systems.
Good:
path = os.path.join(folder_path, name)
Less reliable:
path = folder_path + "/" + name
If you want a deeper explanation, see os.path.join() explained.
Example output
Total size: 15432 bytes
Recursive version: include subfolders
The basic version only counts files in the current folder. If you want the total size of a folder and everything inside it, including subfolders, use os.walk().
import os
folder_path = "."
total_size = 0
for root, dirs, files in os.walk(folder_path):
for name in files:
path = os.path.join(root, name)
total_size += os.path.getsize(path)
print(f"Total size: {total_size} bytes")
How this version works
os.walk(folder_path)goes through the folder and all subfoldersrootis the current folder being processedfilesis a list of file names in that folder- Each file path is built with
os.path.join(root, name) - The script adds the size of every file it finds
This version is usually more useful for real folder size checks.
Show the size in a readable format
File sizes in bytes are correct, but they are not always easy to read. You can add a helper function to show sizes in KB, MB, or GB.
import os
def format_size(size_in_bytes):
units = ["bytes", "KB", "MB", "GB", "TB"]
size = float(size_in_bytes)
for unit in units:
if size < 1024 or unit == units[-1]:
return f"{size:.2f} {unit}"
size /= 1024
folder_path = "."
total_size = 0
for root, dirs, files in os.walk(folder_path):
for name in files:
path = os.path.join(root, name)
total_size += os.path.getsize(path)
print(f"Folder: {folder_path}")
print(f"Total size: {format_size(total_size)}")
Example output
Folder: .
Total size: 2.45 MB
The helper function keeps the conversion logic separate from the main loop, which makes the script easier to read.
Common problems and how to avoid them
Here are the most common reasons this script does not work as expected:
- The folder path is wrong
- The script counts only the current folder, not subfolders
- File paths are built incorrectly
- A permission error blocks access to some files
- The output is shown only in bytes when you expected KB or MB
1. The folder path does not exist
If the path is wrong, Python may raise an error such as FileNotFoundError.
Check the path first:
import os
folder_path = "my_folder"
print(folder_path)
print(os.path.exists(folder_path))
If False is printed, the path is not valid from the script's current location.
For help with this error, see how to fix FileNotFoundError: Errno 2 No such file or directory.
2. You expected subfolders to be included
This is a very common mistake. The os.listdir() version only checks one folder level.
If you want subfolders too, switch to os.walk().
3. Paths are built incorrectly
Always combine paths with os.path.join().
You can debug by printing the path:
import os
folder_path = "."
for name in os.listdir(folder_path):
path = os.path.join(folder_path, name)
print(path)
You can also learn more in how to list files in a directory in Python and os.listdir() explained.
4. A folder is passed to getsize()
os.path.getsize() works on paths, but in this kind of script you usually want file sizes only.
Check with os.path.isfile(path) first:
import os
folder_path = "."
for name in os.listdir(folder_path):
path = os.path.join(folder_path, name)
print(os.path.isfile(path))
5. Permission errors stop the script
Some files may not be accessible. In that case, you may get PermissionError.
A simple way to make the script more robust is to skip files that cannot be read:
import os
folder_path = "."
total_size = 0
for root, dirs, files in os.walk(folder_path):
for name in files:
path = os.path.join(root, name)
try:
total_size += os.path.getsize(path)
except PermissionError:
print(f"Skipped: {path}")
print(f"Total size: {total_size} bytes")
If this happens often, read how to fix PermissionError: Errno 13 Permission denied.
Helpful debugging prints
When the result looks wrong, these simple checks can help:
print(folder_path)
print(os.path.exists(folder_path))
print(os.path.isfile(path))
print(os.path.getsize(path))
print(root, files)
Ways to improve the script
Once the basic version works, you can extend it in useful ways:
- Ask the user for a folder path with
input() - Skip hidden files if needed
- Show the largest files in the folder
- Print separate totals for each subfolder
Here is one simple improvement that lets the user choose the folder:
import os
folder_path = input("Enter a folder path: ")
total_size = 0
for root, dirs, files in os.walk(folder_path):
for name in files:
path = os.path.join(root, name)
total_size += os.path.getsize(path)
print(f"Total size: {total_size} bytes")
You could also combine this with the readable size function from the earlier section.
FAQ
Does this script include subfolders?
The basic version does not. Use a recursive version with os.walk() to include subfolders.
Why am I getting FileNotFoundError?
The path may be wrong, or the current working directory may not be what you expect.
Why is the size shown in bytes?
os.path.getsize() returns bytes. You can convert that value to KB, MB, or GB.
Can I calculate folder size with pathlib instead of os?
Yes, but this example keeps things simple by using os tools that many beginners learn first.
See also
- Python os module overview
- os.listdir() function explained
- os.path.join() function explained
- How to list files in a directory in Python
- How to check if a file exists in Python
- Working with file paths in Python
Try extending this example next by adding subfolder support, readable file sizes, or a report that shows the largest files.