atomicwriter
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.
Usage
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()
Installation
atomicwriter
is available on PyPI, so you can simply use pip to install it.
Building from source
Building from source requires the Rust toolchain and Python 3.9+.
- With
uv
:
- With
pypa/build
:
Acknowledgements
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.
License
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.
Contributing
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.