Dev Helpers
← Back

Swap a staged file into place

Swap a staged file into place in Python.

Open this example in the interactive library

Source code

from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
    final = Path(folder) / 'settings.json'
    staged = final.with_suffix('.tmp')
    staged.write_text('{"ready":true}', encoding='utf-8')
    staged.replace(final)
    print(final.read_text(encoding='utf-8'))

Sample output

{"ready":true}