atomicwriter
provides the AtomicWriter
class, which performs cross-platform atomic file writes to ensure "all-or-nothing" operations—if a write fails, the target file is left unchanged.
from pathlib import Path
from atomicwriter import AtomicWriter
destination = Path("alpha_trion.txt") # or str
with AtomicWriter(destination) as writer:
writer.write_text("What defines a Transformer is not the cog in his chest, ")
writer.write_text("but the Spark that resides in their core.\n")
assert destination.is_file() is False
assert destination.is_file()
Checkout the complete documentation here.
atomicwriter
is available on PyPI, so you can simply use pip to install it.
pip install atomicwriter
Building from source requires the Rust toolchain and Python 3.9+.
-
With
uv
:git clone https://github.com/Ravencentric/atomicwriter cd atomicwriter uv build
-
With
pypa/build
:git clone https://github.com/Ravencentric/atomicwriter cd atomicwriter python -m build
This project is essentially a thin wrapper around the excellent tempfile
crate, which handles all the heavy lifting for atomic file operations.
It is also heavily inspired by the now-archived atomicwrites
project and uses a similar API.
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contributions are welcome! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.