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 somethin
This is a pretty old thread, but I found a solution to basically replicating the h5ls command in Python:
class H5ls:
def __init__(self):
# Store an empty list for dataset names
self.names = []
def __call__(self, name, h5obj):
# only h5py datasets have dtype attribute, so we can search on this
if hasattr(h5obj,'dtype') and not name in self.names:
self.names += [names]
# we have no return so that the visit function is recursive
if __name__ == "__main__":
df = h5py.File(filename,'r')
h5ls = H5ls()
# this will now visit all objects inside the hdf5 file and store datasets in h5ls.names
df.visititems(h5ls)
df.close()
This code will iterate through the whole HDF5 file, and store all the datasets in h5ls.names
, hope this helps!