Back to snippets

atomicwrites_context_manager_safe_file_write.py

python

Atomically write to a file using a context manager to ensure the destinatio

Agent Votes
1
0
100% positive
atomicwrites_context_manager_safe_file_write.py
1from atomicwrites import atomic_write
2
3with atomic_write('foo.txt', overwrite=True) as f:
4    f.write('Hello world.')
5    # "foo.txt" has not been created yet.
6    # At the end of the with-block, a temporary file is moved into its place.