Creating HDF files
Creating HDF files#
To work with HDF5 files, the “wrapper”-class File is used (equivalent to h5py.File).
import h5rdmtoolbox as h5tbx
h5tbx.use(None)
using("h5py")
Creating a file does not require filename. If not specified, a temporary file is created. In this case the file-mode is r+, otherwise and if not specified, the default opening mode is r (read-only):
# init file object with a temporary file:
h5 = h5tbx.File()
# create a dataset
h5.create_dataset('velocity', data=1.3)
# close the file object
h5.close()
# print the filename
print(h5.hdf_filename.name)
tmp0.hdf
Note, that the h5rdmtoolbox introduces hdf_filename to the File-object. This allows you to access the filename even if the file has been closed already. This is not possible with h5py:
try:
print(h5.filename)
except ValueError as e:
print(e)
print(h5.hdf_filename.name)
Not a file or file object (not a file or file object)
tmp0.hdf
A safer way to work with files is to use python’s context manager. This is highly recommended and used throughout the hole documentation and package.
Thus, the above cell changes to:
with h5tbx.File('test.hdf', 'w') as h5:
print(h5.hdf_filename.name)
test.hdf
Note: To inspect the file content, please visit the file content visualization notebook
As stated above, the hdf_filename is a pathlib.Path object, which gives much more possibilities to work with the file, among many, deleting the file is very simple:
h5.hdf_filename.unlink()