treesize - File/Folder Sizes (original) (raw)
treesize — Get Sizes of all Files and Folders in a Tree
This page hosts treesize, a Python command-line script that you can use to isolate large files and folders taking up space on your devices. This script also demos recursion and basic file interfaces in Python.
Overview
This modest Python 3.X program generates a report giving the total space taken by each folder and file in a directory tree, grouped by type and ordered by size. This includes all the subfolders nested in the tree; each folder's size is computed from its contents recursively. After a run, search the report for "[Directories]" and "[Files]" to find your largest items.
This script's main goal is to help locate candidates for removal when reclaiming disk space; navigating with file explorer GUIs tends to be tedious (or unusable) in this role. Its report file also provides a snapshot of the contents of your folders or drives. While there are tools to do similar work on some platforms, treesize is a portable option that works everywhere that Python does.
Resources
See the main script's docstring and other items below for usage details.
Code and docs:
- treesize.py — the script's complete code (188 lines, 40% docs)
Examples:
- Report file — generated output, Mac OS
- Report file — generated output, Windows
- Interactive mode — inputs via interactive prompts
- Command lines — inputs via command-line arguments
- Command lines — both input modes, Mac OS
Download
Use the following to fetch treesize, or view its unzipped content.
- treesize-full-package.zip — with all code and examples
- Unpacked content — zipfile content online
This program was last changed: June 2017. It was patched Jan-2025 for Python string-escape deprecations.
Recent upgrades: treesize includes a minor update to skip symlinks on platforms that support them, so the size tally isn't inflated artificially. Symlinks are rare on Windows, but not on Mac OS or Linux. See the script's docstring for details. This script has also now been shown to handle non-ASCII filenames properly on all Python/platform combinations tested (without the perils of its CGI-script comrades here and here).
For more code examples, see theprograms page.