fields.buffers.core.HDF5Buffer.open#
- classmethod HDF5Buffer.open(file: str | Path | File, path: str, *, mode: str = 'r+', close_on_exit: bool = False) HDF5Buffer [source]#
Open an existing HDF5 dataset and return it as a buffer.
This method provides safe access to datasets within an HDF5 file, whether the file is passed as a string path or an already opened
h5py.File
object.When used in a with statement (context manager), the file will be automatically flushed and closed at exit if
close_on_exit=True
.- Parameters:
file (
str
,Path
, orh5py.File
) – File path or an open HDF5 file containing the dataset.path (
str
) – Path to the dataset inside the HDF5 file (e.g.,"/my/data"
).mode (
str
, default"r+"
) –File mode to use when file is a path. Ignored if file is a
h5py.File
. Common values:"r"
: read-only"r+"
: read/write"a"
: read/write or create
close_on_exit (
bool
, defaultFalse
) – If True, the file will be closed when the buffer is used in a context manager.
- Returns:
A buffer object wrapping the specified dataset.
- Return type:
- Raises:
FileNotFoundError – If the file or dataset path does not exist.
TypeError – If file is neither a path nor an h5py.File.
Examples
>>> with HDF5Buffer.open("data.h5", "temperature", close_on_exit=True) as buf: ... print(buf.shape)