| name | file-structure |
| description | Create and manage directory structures. Use for scaffolding projects or organizing files. |
File Structure
Create and manage directory structures programmatically.
Quick Start
from pathlib import Path
# Create directory
Path("src/utils").mkdir(parents=True, exist_ok=True)
# Create file
Path("src/utils/__init__.py").touch()
# Create with content
Path("src/main.py").write_text("# Main module\n")
Common Patterns
Create project structure
def create_structure(base: str, structure: dict):
base_path = Path(base)
for name, content in structure.items():
path = base_path / name
if isinstance(content, dict):
path.mkdir(parents=True, exist_ok=True)
create_structure(str(path), content)
else:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content or "")
structure = {
"src": {
"__init__.py": "",
"main.py": "# Entry point",
"utils": {"__init__.py": "", "helpers.py": ""}
},
"tests": {"__init__.py": ""},
"README.md": "# Project"
}
create_structure("myproject", structure)
List directory tree
def list_tree(path: Path, prefix: str = "") -> list[str]:
lines = []
items = sorted(path.iterdir())
for item in items:
lines.append(f"{prefix}{item.name}")
if item.is_dir():
lines.extend(list_tree(item, prefix + " "))
return lines
Clean directories
import shutil
def clean_dir(path: Path, keep: list[str] = None):
keep = keep or []
for item in path.iterdir():
if item.name not in keep:
if item.is_dir():
shutil.rmtree(item)
else:
item.unlink()