fileio

File I/O module.

exception wmflib.fileio.FileIOError[source]

Bases: WmflibError

Custom exception class for errors of the BlockInFile class.

exception wmflib.fileio.LockError[source]

Bases: FileIOError

Custom exception class raised when unable to exclusively lock a file.

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.