finesse.utilities.misc.opened_file

finesse.utilities.misc.opened_file(fobj, mode)[source]

Get an open file regardless of whether a string or an already open file is passed.

Parameters

fobj : str or file-like

The path or file object to ensure is open. If fobj is an already open file object, its mode is checked to be correct but is otherwise returned as-is. If fobj is a string, it is opened with the specified mode and yielded, then closed once the wrapped context exits. Note that passed open file objects are not closed.

mode : str

The mode to ensure fobj is opened with.

Yields

io.FileIO

The open file with the specified mode.

Raises

ValueError

If fobj is not a string nor open file, or if fobj is open but with a different mode.