import contextlib import os import tempfile from typing import Iterator @contextlib.contextmanager def temp_audio(data: bytes, suffix: str) -> Iterator[str]: fd, path = tempfile.mkstemp(suffix=suffix, prefix="fd_") try: with os.fdopen(fd, "wb") as f: f.write(data) yield path finally: with contextlib.suppress(OSError): os.remove(path)