Files
jarvis/capabilities/file_system.py

44 lines
1.4 KiB
Python
Raw Normal View History

2026-03-24 00:11:34 -05:00
"""
File system operations create projects, manage folders, list files.
Cross-platform: uses pathlib and os only.
"""
import os
import subprocess
from pathlib import Path
def create_project(name: str, base_path: str = "C:/Projects") -> str:
"""Create a new project folder with README and .gitignore."""
path = Path(base_path) / name
path.mkdir(parents=True, exist_ok=True)
(path / "README.md").write_text(f"# {name}\n\nProject created by JARVIS.\n")
(path / ".gitignore").write_text("__pycache__/\n.env\n*.pyc\n")
return str(path)
def list_directory(path: str) -> list[dict]:
"""List files and folders in a directory."""
p = Path(path)
if not p.exists():
return []
return [
{'name': item.name, 'type': 'dir' if item.is_dir() else 'file', 'size': item.stat().st_size if item.is_file() else None}
for item in sorted(p.iterdir())
]
def read_file(path: str) -> str:
"""Read the contents of a text file."""
return Path(path).read_text(encoding='utf-8')
def write_file(path: str, content: str):
"""Write content to a file, creating parent directories if needed."""
p = Path(path)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(content, encoding='utf-8')
def delete_file(path: str):
"""Delete a file."""
Path(path).unlink(missing_ok=True)
def open_folder(path: str):
"""Open a folder in Windows Explorer."""
os.startfile(path)