Skip to content

Commit ce24464

Browse files
authored
Merge pull request #498 from arceuzvx/disk-visualizer
Disk usage visualizer
2 parents d5f0379 + 97652f5 commit ce24464

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Disk Usage Visualizer/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Disk Usage Visualizer (CLI)
2+
3+
A simple Python script that scans directories and displays the largest folders or files by disk usage — right from your terminal.
4+
Lightweight, fast, and cross-platform (works on Linux, macOS, and Windows).
5+
6+
## Features
7+
8+
- Recursively analyzes disk usage in a directory
9+
10+
- Displays top N largest folders (default: 10)
11+
12+
- Handles long file paths and permission errors gracefully
13+
14+
- Works seamlessly on Windows, Linux, and macOS
15+
16+
## Usage
17+
18+
```bash
19+
# Analyze current directory
20+
python disk_visualizer.py
21+
22+
# Analyze a specific path
23+
python disk_visualizer.py D:\Projects # Windows
24+
python disk_visualizer.py /home/user/docs # Linux/macOS
25+
26+
# Show top 20 largest folders
27+
python disk_visualizer.py . -n 20
28+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
3+
def get_dir_sizes(path="."):
4+
dir_sizes = {}
5+
for root, dirs, files in os.walk(path):
6+
for name in files:
7+
try:
8+
full_path = os.path.join(root, name)
9+
10+
# Handle long paths in Windows
11+
if os.name == "nt":
12+
full_path = r"\\?\\" + os.path.abspath(full_path)
13+
14+
size = os.path.getsize(full_path)
15+
dir_path = os.path.relpath(root, path)
16+
dir_sizes[dir_path] = dir_sizes.get(dir_path, 0) + size
17+
except (FileNotFoundError, PermissionError, OSError):
18+
continue
19+
return dir_sizes
20+
21+
22+
def main():
23+
import argparse
24+
parser = argparse.ArgumentParser(description="Disk Usage Visualizer (CLI)")
25+
parser.add_argument("path", nargs="?", default=".", help="Path to analyze")
26+
parser.add_argument(
27+
"-n", "--top", type=int, default=10, help="Show top N largest directories"
28+
)
29+
args = parser.parse_args()
30+
31+
print(f"Analyzing disk usage in: {args.path}\n")
32+
33+
dir_sizes = get_dir_sizes(args.path)
34+
35+
# Sort by size (descending)
36+
sorted_dirs = sorted(dir_sizes.items(), key=lambda x: x[1], reverse=True)
37+
38+
print(f"Top {args.top} largest directories:")
39+
print("-" * 60)
40+
print(f"{'Directory':40s} | {'Size (MB)':>10s}")
41+
print("-" * 60)
42+
43+
for dir_path, size in sorted_dirs[: args.top]:
44+
size_mb = size / (1024 * 1024)
45+
print(f"{dir_path:40s} | {size_mb:10.2f}")
46+
47+
print("-" * 60)
48+
print("✅ Disk usage analysis complete.")
49+
50+
51+
if __name__ == "__main__":
52+
main()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ More information on contributing and the general code of conduct for discussion
5656
| 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. |
5757
| 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. |
5858
| 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. |
59+
| 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.
5960
| 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. |
6061
| 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.
6162
| 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. |

0 commit comments

Comments
 (0)