fileio
File I/O module.
- exception wmflib.fileio.FileIOError[source]
Bases:
WmflibErrorCustom exception class for errors of the BlockInFile class.
- __annotations__ = {}
- __doc__ = 'Custom exception class for errors of the BlockInFile class.'
- __module__ = 'wmflib.fileio'
- exception wmflib.fileio.LockError[source]
Bases:
FileIOErrorCustom exception class raised when unable to exclusively lock a file.
- __annotations__ = {}
- __doc__ = 'Custom exception class raised when unable to exclusively lock a file.'
- __module__ = 'wmflib.fileio'
- wmflib.fileio.locked_open(file_path: PathLike, file_mode: str = 'r', *, timeout: int = 10) Generator[IO, None, None][source]
Context manager to open a file with an exclusive lock on it and a retry logic.
Examples
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")
- Parameters:
file_path (os.PathLike) – the file path to open.
file_mode (str, optional) – the mode in which the file is opened, see
open()for details.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.
- Raises:
wmflib.fileio.LockError – on failure to acquire the exclusive lock on the file.
- Yields:
file object – the open file with an exclusive lock on it.