Deprecation notice for python-atomicwrites
masterWarning: This package is unmaintained.
The maintainer has deprecated this package. For most use cases, Python 3's built-in os.replace and os.rename are sufficient alternatives.
repository·master·Indexed 43 days ago
https://github.com/untitaker/python-atomicwritesA high-level API for performing atomic file writes across POSIX and Windows platforms to prevent data corruption. Provides the `atomic_write` function, a low-level API including `replace_atomic`, `move_atomic`, and the `AtomicWriter` class. Note: This package is currently unmaintained.
The maintainer has deprecated this package. For most use cases, Python 3's built-in os.replace and os.rename are sufficient alternatives.
The library ensures atomicity by following these steps:
rename if overwrite=True. If overwrite=False, it uses a combination of link and unlink.MoveFileEx via ctypes with appropriate flags.fsync on the temporary file (to flush content/metadata) and on the parent directory (to flush the filename). On OS X, fcntl with F_FULLFSYNC is used._commit.Important Notes:
chmod to preserve permissions.link/unlink on POSIX, there is a brief window where the file may appear under both the temporary name and the target name.All filesystem-related errors raised by atomicwrites are subclasses of OSError.
ctypes implementation.In both cases, you can inspect the errno attribute on the caught exception to identify the specific error code, which maps to the standard errno module.
The atomic_write function is the primary high-level API for writing data to a file atomically. It ensures that the file is either fully written or not written at all, preventing data corruption in case of crashes or power failures.
from atomicwrites import atomic_write
with atomic_write('foo.txt', mode='w') as f:
f.write('some data')Use the atomic_write function to ensure that a file is written completely or not at all. This is achieved by writing to a temporary file in the same directory and then moving it to the target location.
By default, atomic_write will fail if the target file already exists. To allow overwriting the existing file, set the overwrite=True parameter.
from atomicwrites import atomic_write
# To overwrite an existing file:
with atomic_write('foo.txt', overwrite=True) as f:
f.write('Hello world.')
# To fail if the file already exists (default behavior):
with atomic_write('foo.txt') as f:
f.write('Hello world.')For more granular control, atomicwrites provides a low-level API including:
replace_atomic: Performs an atomic replacement of a file.move_atomic: Performs an atomic move operation.AtomicWriter: A class providing manual control over the atomic writing process.