question 229 CLI & Automation · medium
Disk usage by top-level folder
A clean-up script runs du -k in a repository checkout and reads its output: one line per folder, the size in kilobytes, a tab, and the path. Every folder's size already includes its subfolders. Turn the output into the size of each top-level folder.
Function
top_level_sizes(du_output)du_outputtext like
"12\t./src/app\n40\t./src\n8\t./docs\n61\t.\n"Returnsa dictionary from top-level folder name (
src, docs, .github) to its size in KB as an intRulesuse only lines whose path is exactly
./<name>; deeper lines would double-count and the . line is the total; blank lines are ignoredExamples
calltop_level_sizes('12\t./src/app\n40\t./src\n8\t./docs\n3\t./.github/workflows\n5\t./.github\n61\t.\n')returns{'src': 40, 'docs': 8, '.github': 5}
calltop_level_sizes('')returns{}
calltop_level_sizes('4\t./build\n4\t.\n')returns{'build': 4}