← Lesson
with, Files, and Paths
Joeven
Run
Reset
Python loads on first run
from pathlib import Path class Box: def __enter__(self): print("open") return self def __exit__(self, *unused): print("close") return False with Box(): print("work") p = Path("agent-note.txt") p.write_text(chr(10).join(["goal: find weather", "step: 1", ""]), encoding="utf-8") print("name", p.name) print("exists", p.exists()) print("text:") print(p.read_text(encoding="utf-8")) print("as json name", p.with_suffix(".json").name) raw = "café".encode("utf-8") print("bytes", raw) print("back to text", raw.decode("utf-8")) p.unlink() print("deleted", not p.exists())
Run to execute this in your browser. Nothing is sent to a server.