File I/O module.
- exception wmflib.fileio.FileIOError¶
Custom exception class for errors of the BlockInFile class.
- exception wmflib.fileio.LockError¶
Custom exception class raised when unable to exclusively lock a file.
- wmflib.fileio.locked_open(file_path: os.PathLike, file_mode: str = 'r', *, timeout: int = 10) Generator[IO, None, None] ¶
Context manager to open a file with an exclusive lock on it and a retry logic.
from wmflib.fileio import locked_open with locked_open('existing.file') as f: text = f.read() with locked_open('new.out', 'w') as f: f.write('Some text')
file_path (os.PathLike) – the file path to open.
timeout (int, optional) – the total timeout in seconds to wait to acquire the exclusive lock before giving up. Ten tries will be attempted to acquire the lock within the timeout.
wmflib.fileio.LockError – on failure to acquire the exclusive lock on the file.
file object – the open file with an exclusive lock on it.