finesse.utilities.misc.opened_file

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

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

Adapted from numpy.loadtxt().

Parameters:
filenamestr, pathlib.Path, or file-like

The path or file object to ensure is open. If filename is an already open file object, it is yielded as-is, and is not closed after the wrapped context exits. If filename is a string, it is opened with the specified mode and yielded, then closed once the wrapped context exits.

modestr

The mode to open filename with, if it is not already open.

Yields:
io.FileIO

The open file with the specified mode.

Notes

If filename is an open file, mode is ignored; it is the responsibility of the calling code to check that it is opened with the correct mode.