How do I traverse all the groups and datasets of an hdf5 file using h5py?
I want to retrieve all the contents of the file from a common root using a for loop or something similar.
visit()
and visititems()
are your friends here. Cf. http://docs.h5py.org/en/latest/high/group.html#Group.visit. Note that an h5py.File
is also an h5py.Group
. Example (not tested):
def visitor_func(name, node):
if isinstance(node, h5py.Dataset):
# node is a dataset
else:
# node is a group
with h5py.File('myfile.h5', 'r') as f:
f.visititems(visitor_func)