Skip to content

Instantly share code, notes, and snippets.

View ADemkin's full-sized avatar

Anton Demkin ADemkin

  • Tbilisi, Geogia
  • 00:48 (UTC +04:00)
View GitHub Profile
from contextlib import contextmanager
from os import rename
from tempfile import NamedTemporaryFile
from typing import BinaryIO
@contextmanager
def atomic_write(path: str, mode: str = 'w', encoding: str = 'utf-8') -> BinaryIO:
temp_file = NamedTemporaryFile(mode=mode, encoding=encoding, delete=False)
try: