Skip to main content

dirsize

Size of a directory tree, apparent and on disk, as typed calls and a du-like command. Hard links count once, symlinks and Windows junctions are never followed by surprise, and unreadable folders are reported instead of ending the scan.

Install

pip install dirsize

Usage

from dirsize import scan

usage = scan("my_project")
print(usage.disk_bytes, usage.apparent_bytes, usage.files, usage.dirs)
for error in usage.errors:
    print("skipped:", error.path, error.kind, error.message)

disk_bytes is the space the files take on the disk. apparent_bytes is the sum of their sizes, what ls -l shows. They differ for sparse and compressed files, and because space is allocated in whole blocks.

Every directory, as it is measured

from dirsize import iter_dirs

for folder in iter_dirs("my_project"):
    print(folder.disk_bytes, folder.path)

Directories stream children first and the root last, like du without -s. Each DirUsage carries the totals for its whole subtree and its depth.

The largest files or folders

from dirsize import largest

for item in largest("my_project", 5):
    print(item.disk_bytes, item.path)

for item in largest("my_project", 3, kind="dir", by="apparent"):
    print(item.apparent_bytes, item.path)

Only n items are kept in memory while the tree is walked.

Options

from dirsize import scan

scan("my_project", follow_symlinks=True)  # enter symlinked folders and junctions
scan("my_project", one_filesystem=True)  # skip folders on another device, like du -x
scan("my_project", count_links=True)  # count every hard link, like du -l
scan("my_project", include_dirs=True)  # add the blocks folders take, like du
scan("my_project", on_error="raise")  # stop at the first unreadable path

iter_dirs takes the same options; largest takes all but include_dirs. on_error may also be a callable that receives each OSError as it happens.

The simple calls

from dirsize import dir_size, file_count

print(dir_size("my_project"))  # apparent bytes, hard links once
print(file_count("my_project"))  # one per name, links included

These two raise on the first unreadable path unless on_error="ignore" or a callable is given, and they skip link loops silently.

Command line

dirsize [-s] [-A] [-h] [-x] [-L] [-l] [--top N] [--json] [PATH ...]

Without options it prints the disk usage in bytes of every directory, children first, like du. -s prints one total per path, -A uses apparent sizes, -h prints sizes such as 1.5K or 20M (rounded up, as du -h does), --top N lists the N largest files, and --json prints one document with the totals and errors of each path; these three are exclusive. Like du, a hard link or a folder shared by several paths counts only under the first. Errors go to standard error and the exit status is 1. The same tool runs as python -m dirsize.

What is counted

  • Hard links. A file with several names counts once, in the folder where it is met first, unless count_links=True. Every name still counts as a file.
  • Links. A symlink below the root, or a Windows junction or other reparse point that names another path, counts as a link entry of its own size and is not entered. With follow_symlinks=True its target is measured instead. The root path is always resolved.
  • Cycles. A folder is entered at most once per device and file id. Like du -L, a followed link back to a parent is simply not entered again; a physical loop (a bind mount or a hard-linked folder) is reported as cycle. When following links, a dangling one is a missing error, as in du -L.
  • Disk usage. On Linux and macOS it is st_blocks * 512. On Windows it is the allocation NTFS reports for the file (AllocationSize), which already accounts for compression, sparse ranges and tiny files kept inside the file table; links and folders take 0 there.
  • Apparent size. Only regular files and symlinks have one, as in GNU du; folders, devices and pipes add 0.
  • Errors. Each one becomes a ScanError with the path, a kind (permission, missing, cycle, other) and the system message.

With include_dirs=True the totals match GNU du -sb (apparent) and du -sB1 (disk) on Linux; the test suite checks this against GNU du on generated trees.

Why

The standard library has no single call for the size of a directory tree. The common recipe, summing os.path.getsize over os.walk, double counts hard links, ignores what sparse and compressed files really take, walks into junctions on Windows and can loop forever on a link cycle. dirsize is the du primitive as typed functions: one os.scandir walk, (st_dev, st_ino) for hard links and cycles, and every error kept next to the result. No dependencies, mypy --strict clean.

License

MIT. See LICENSE.

Metadata

Release files for dirsize 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dirsize 0.2.0
File Size Uploaded Provenance Actions
dirsize-0.2.0.tar.gz 27.9 kB Without provenance Details for dirsize-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dirsize 0.2.0
File Interpreter ABI Platform Actions
dirsize-0.2.0-py3-none-any.whl Python 3 none any Details for dirsize-0.2.0-py3-none-any.whl

Total release size: 42.6 kB

Release files / dirsize-0.2.0.tar.gz

Download URL dirsize-0.2.0.tar.gz
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
68f1937f7f702d95eabeb4a3c933e882ff0aa7ce47a3b2202b443e8d0fcb6f2a
BLAKE2b-256 checksum
How to use checksums
7a317b9cc702c51a4722fb01135e58fda3cd6bfec2b382040d06f82f66bbd4b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / dirsize-0.2.0-py3-none-any.whl

Download URL dirsize-0.2.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b671662e2a515c823f2af86db074d38bdfe4ef260bf4a58cd07ddba186da50ae
BLAKE2b-256 checksum
How to use checksums
269a43fcbe65b65d6085a7f5a0e4c8bc3e565860af6ad2d2460b641886e29a24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for dirsize 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page