safe_save(path, text) that writes text to a temporary file path + '.tmp' first and only then atomically renames it over the original with os.replace — the standard write-temp-then-swap pattern.safe_save('settings.ini', 'mode=dark')settings.ini now holds mode=dark, never a partial write
The rename is atomic; readers see either the old or the new file, never a torn one.
Close the temp file before renaming (leave the with block).
os.replace works even when the target exists.