diff --git a/Disk Usage Visualizer/README.md b/Disk Usage Visualizer/README.md new file mode 100644 index 00000000..a2c80bc5 --- /dev/null +++ b/Disk Usage Visualizer/README.md @@ -0,0 +1,28 @@ +# Disk Usage Visualizer (CLI) + +A simple Python script that scans directories and displays the largest folders or files by disk usage — right from your terminal. +Lightweight, fast, and cross-platform (works on Linux, macOS, and Windows). + +## Features + +- Recursively analyzes disk usage in a directory + +- Displays top N largest folders (default: 10) + +- Handles long file paths and permission errors gracefully + +- Works seamlessly on Windows, Linux, and macOS + +## Usage + +```bash +# Analyze current directory +python disk_visualizer.py + +# Analyze a specific path +python disk_visualizer.py D:\Projects # Windows +python disk_visualizer.py /home/user/docs # Linux/macOS + +# Show top 20 largest folders +python disk_visualizer.py . -n 20 +``` diff --git a/Disk Usage Visualizer/disk_visualizer.py b/Disk Usage Visualizer/disk_visualizer.py new file mode 100644 index 00000000..053e170f --- /dev/null +++ b/Disk Usage Visualizer/disk_visualizer.py @@ -0,0 +1,52 @@ +import os + +def get_dir_sizes(path="."): + dir_sizes = {} + for root, dirs, files in os.walk(path): + for name in files: + try: + full_path = os.path.join(root, name) + + # Handle long paths in Windows + if os.name == "nt": + full_path = r"\\?\\" + os.path.abspath(full_path) + + size = os.path.getsize(full_path) + dir_path = os.path.relpath(root, path) + dir_sizes[dir_path] = dir_sizes.get(dir_path, 0) + size + except (FileNotFoundError, PermissionError, OSError): + continue + return dir_sizes + + +def main(): + import argparse + parser = argparse.ArgumentParser(description="Disk Usage Visualizer (CLI)") + parser.add_argument("path", nargs="?", default=".", help="Path to analyze") + parser.add_argument( + "-n", "--top", type=int, default=10, help="Show top N largest directories" + ) + args = parser.parse_args() + + print(f"Analyzing disk usage in: {args.path}\n") + + dir_sizes = get_dir_sizes(args.path) + + # Sort by size (descending) + sorted_dirs = sorted(dir_sizes.items(), key=lambda x: x[1], reverse=True) + + print(f"Top {args.top} largest directories:") + print("-" * 60) + print(f"{'Directory':40s} | {'Size (MB)':>10s}") + print("-" * 60) + + for dir_path, size in sorted_dirs[: args.top]: + size_mb = size / (1024 * 1024) + print(f"{dir_path:40s} | {size_mb:10.2f}") + + print("-" * 60) + print("✅ Disk usage analysis complete.") + + +if __name__ == "__main__": + main() diff --git a/README.md b/README.md index d2635b7e..9065422b 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ More information on contributing and the general code of conduct for discussion | CSV_TO_NDJSON | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/main/CSV_TO_NDJSON) | A Python script to convert a CSV to an NDJSON files file. | | Currency Script | [Currency Script](https://github.com/DhanushNehru/Python-Scripts/tree/main/Currency%20Script) | A Python script to convert the currency of one country to that of another. | | Digital Clock | [Digital Clock](https://github.com/DhanushNehru/Python-Scripts/tree/main/Digital%20Clock) | A Python script to preview a digital clock in the terminal. | +| Disk Usage Visualizer | [Disk Usage Visualizer](https://github.com/DhanushNehru/Python-Scripts/tree/main/Disk%20Usage%20Visualizer) | A Python script to display the top N directories taking up space in your disk. | Display Popup Window | [Display Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/main/Display%20Popup%20Window) | A Python script to preview a GUI interface to the user. | | Distance Calculator | [Distance Calculator](https://github.com/Mathdallas-code/Python-Scripts/tree/main/Distance%20Calculator) | A Python script to calculate the distance between two points. | Duplicate Finder | [Duplicate Finder](https://github.com/DhanushNehru/Python-Scripts/tree/main/Duplicate%Fnder) | The script identifies duplicate files by MD5 hash and allows deletion or relocation. |