| |
| import os, sys, math, hashlib |
| from pathlib import Path |
|
|
| def human(n): |
| units = ["B","KB","MB","GB","TB","PB"] |
| if n == 0: return "0 B" |
| k = int(math.floor(math.log(n, 1024))) |
| return f"{n/1024**k:.2f} {units[k]}" |
|
|
| def dir_size(path: Path) -> int: |
| total = 0 |
| for p in path.rglob("*"): |
| if p.is_file(): |
| try: |
| total += p.stat().st_size |
| except Exception: |
| pass |
| return total |
|
|
| def main(): |
| if len(sys.argv) < 2: |
| print("Usage: python dataset_size.py <root_dir>") |
| sys.exit(1) |
| root = Path(sys.argv[1]).resolve() |
| if not root.exists() or not root.is_dir(): |
| print(f"Not a directory: {root}") |
| sys.exit(1) |
|
|
| |
| subs = [p for p in root.iterdir() if p.is_dir()] |
| rows = [] |
| grand = 0 |
| for s in subs: |
| sz = dir_size(s) |
| grand += sz |
| rows.append((s.name, sz)) |
| |
| root_files = sum((p.stat().st_size for p in root.iterdir() if p.is_file()), 0) |
| grand += root_files |
| if root_files: |
| rows.append(("(files in root)", root_files)) |
|
|
| rows.sort(key=lambda x: x[1], reverse=True) |
|
|
| print(f"\nDataset: {root}") |
| print("-"*70) |
| for name, sz in rows: |
| print(f"{name:<30} {human(sz):>12}") |
| print("-"*70) |
| print(f"{'TOTAL':<30} {human(grand):>12}") |
|
|
| if __name__ == "__main__": |
| main() |
|
|