HDF5 files work generally like standard Python file objects. They support standard modes like r/w/a, and should be closed when they are no longer in use. However, there is obviously no concept of “text” vs “binary” mode.
>>> f = h5py.File('myfile.hdf5','r')
The file name may be either str or unicode. Valid modes are:
r Readonly, file must exist r+ Read/write, file must exist w Create file, truncate if exists w- Create file, fail if exists a Read/write if exists, create otherwise (default)
HDF5 ships with a variety of different low-level drivers, which map the logical HDF5 address space to different storage mechanisms. You can specify which driver you want to use when the file is opened:
>>> f = h5py.File('myfile.hdf5', driver=<driver name>, <driver_kwds>)
For example, the HDF5 “core” driver can be used to create a purely in-memory HDF5 file, optionally written out to disk when it is closed. See the File class documentation for an exhaustive list.
In addition to the properties and methods defined here, File objects inherit the full API of Group objects; in this case, the group in question is the root group (/) of the file.
Note
Please note that unlike Python file objects, and h5py.File objects from h5py 1.1, the attribute File.name does not refer to the file name on disk. File.name gives the HDF5 name of the root group, “/“. To access the on-disk name, use File.filename.
Represents an HDF5 file on disk.
File(name, mode=None, driver=None, **driver_kwds)
Legal modes: r, r+, w, w-, a (default)
File objects inherit from Group objects; Group-like methods all operate on the HDF5 root group (‘/’). Like Python file objects, you must close the file (“obj.close()”) when you’re done with it. File objects may also be used as context managers in Python “with” blocks.
The HDF5 file driver may also be specified:
Memory-map the entire file; all operations are performed in memory and written back out when the file is closed. Keywords:
Store the file on disk as a series of fixed-length chunks. Useful if the file system doesn’t allow large files. Note: the filename you provide must contain a printf-style integer format code (e.g. %d”), which will be replaced by the file sequence number. Keywords:
memb_size: Maximum file size (default is 2**31-1).
File properties
File methods
Properties common to all HDF5 objects:
Return the parent group of this object.
This is always equivalent to file[posixpath.basename(obj.name)].