Print a sorted directory inventory as JSON
List directory entries with type and size as JSON from a system-info skill script.
#!/usr/bin/env python3
"""Print a sorted directory inventory as JSON."""
import argparse
import json
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Create Directory Inventory
# ---------------------------------------------------------------------------
def create_directory_inventory(path: str) -> dict[str, object]:
"""Describe the direct children of one directory."""
directory = Path(path).expanduser().resolve()
entries: list[dict[str, object]] = []
for entry in directory.iterdir():
is_file = entry.is_file()
entries.append(
{
"is_dir": entry.is_dir(),
"name": entry.name,
"size": entry.stat().st_size if is_file else None,
}
)
entries.sort(key=lambda entry: (not bool(entry["is_dir"]), str(entry["name"])))
return {
"count": len(entries),
"entries": entries,
"path": str(directory),
}
# ---------------------------------------------------------------------------
# Run Directory Inventory Script
# ---------------------------------------------------------------------------
def main() -> int:
"""Parse the optional path and write one machine-readable inventory."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"path",
nargs="?",
default=".",
help="Directory to inspect (defaults to the current directory).",
)
args = parser.parse_args()
try:
result = create_directory_inventory(args.path)
except OSError as exc:
print(
json.dumps(
{"error": str(exc), "path": str(Path(args.path).expanduser())},
indent=2,
sort_keys=True,
),
file=sys.stderr,
)
return 1
print(json.dumps(result, indent=2, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())This is part of the system-info skill bundle. Its location relative to basic.py is sample_skills/system-info/scripts/list_directory.py; keep the bundle's SKILL.md alongside the scripts directory. The linked setup downloads all required files.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateUse the helper
The system-info skill executes this helper as a subprocess and passes the directory path as its first argument.
Full source: cookbook/05_agent_os/23_skills/sample_skills/system-info/scripts/list_directory.py