Analyze a base directory and calculate the total number of files, subfolders, and overall directory size, with optional filtering by one or more file extensions.
Project description
DirStats
dirstats is a lightweight command-line utility written in Python for analyzing directory structures. It reports the total number of files, subfolders, and the aggregate size of a directory, with optional filtering by one or more file extensions.
Built entirely with Python’s standard library, it is fast, portable, and easy to integrate into automation workflows.
Overview
dirstats recursively scans a base directory and collects useful statistics:
- Number of files
- Number of subdirectories
- Total size of files (Bytes, KB, MB, GB)
- Optional filtering by file extension(s)
- It is designed as a small, reusable CLI tool, ideal for:
- File system analysis
- Automation scripts
- Backend utilities
- Data preprocessing
- Learning and teaching filesystem traversal with Python
Installation
Unix / Linux / macOS / Windows
pip install dirstats
After installation, the dirstats command will be available globally.
Usage
Analyze all files in a directory
- dirstats /path/to/directory
Analyze only specific file extensions
- dirstats /path/to/directory py txt md
Analyze all files explicitly
- dirstats /path/to/directory "*"
Alternative Installation (Without pip)
If you prefer not to install dirstats via pip, you can run it directly from the source code by cloning or downloading the repository.
1. Clone or Download the Project
Using Git:
git clone https://github.com/Sharrrkkk/DirStats
cd dirstats
Or download the ZIP from GitHub and extract it. Make sure you have Python 3.10 or higher installed.
Usage Without Installation
The CLI tool can be executed directly using Python by running the main script.
Linux / macOS (Unix-like systems)
python3 src/dirstats/dirstats_run.py <directory_path> [extensions...]
Examples:
Analyze all files in a directory:
python3 src/dirstats/dirstats_run.py /home/user/projects
Analyze only specific file extensions:
python3 src/dirstats/dirstats_run.py /home/user/projects py txt json
Windows
python src\dirstats\dirstats_run.py <directory_path> [extensions...]
Examples:
Analyze all files:
python src\dirstats\dirstats_run.py C:\Users\User\Documents
Analyze specific file extensions:
python src\dirstats\dirstats_run.py C:\Users\User\Documents py txt json
How It Works (Algorithm Overview)
1. Parse CLI arguments
- Base directory path
- Optional file extensions
2. Traverse the directory recursively
- Uses pathlib.Path.glob("**/*")
3. Filter files (if extensions are provided)
4. Collect statistics
- File count
- Subfolder count (All folders, and if one or more extensions were specified, only folders containing files matching those extensions)
- Total file size
5. Display results
- Multiple size units for readability
All operations rely exclusively on Python’s standard library.
Features
- Recursive directory traversal
- Extension-based file filtering
- Accurate size calculation
- Clear CLI output
- No external dependencies
- Cross-platform support
- Minimal and readable codebase
Project Structure
.
├── LICENSE
├── README.md
├── pyproject.toml
└── src
└── dirstats
├── __about__.py
├── __init__.py
└── dirstats_run.py
Notes
-
When filtering by file extensions, only subfolders containing matching files are counted. If no filter is used (i.e., no arguments for extensions or using "*"), all subfolders in the base directory are counted.
-
File extensions must be passed without periods (py, not .py).
-
To analyze the entire base directory, you don't need to specify extensions or use "", but you can use "" to analyze all files without filtering if you want to be more explicit. However, omitting extensions serves the same purpose as using "*".
-
Designed for use with the command line, but it can be imported and extended if needed.
License
- This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dirstats-1.0.1.tar.gz.
File metadata
- Download URL: dirstats-1.0.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a6fffe526f6acd97d19b6228cb5f6872b12f06ca5c09255e5ec07369d366f40
|
|
| MD5 |
583ab07eabf3abdfa98c493f35915738
|
|
| BLAKE2b-256 |
805022dee7ea6bc1d4d293aba4af57da0e477822f01b8e0bb22e3abe07ad683a
|
File details
Details for the file dirstats-1.0.1-py3-none-any.whl.
File metadata
- Download URL: dirstats-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfb65076d1af090c5a0efef30da3d4f6b8258c370878bfb671e66fed93302779
|
|
| MD5 |
d26740034117b0f37460ac132b4ad5d7
|
|
| BLAKE2b-256 |
f0ded47f6f55dbd976ba2b742c7b37c48cabd8cccb6ace3ac795ce83c1dd7a06
|