I have a h5py file storing numpy arrays, but I got Object doesn't exist error
when trying to open it with the dataset name I remember, so is there a way I can list what datasets the file has?
with h5py.File('result.h5','r') as hf:
#How can I list all dataset I have saved in hf?
You have to use the keys method. This will give you a List of unicode strings of your dataset and group names. For example:
Another gui based method would be to use HDFView. https://support.hdfgroup.org/products/java/release/download.html
Just for showing the name of the underlying datasets, I would simply use
h5dump -n <filename>
That is without running a python script.
If you are at the command line, use
h5ls -r [file]
orh5dump -n [file]
as recommended by others.Within python, if you want to list below the topmost group but you don't want to write your own code to descend the tree, try the visit() function:
Or for something more advanced (e.g. to include attributes info) use visititems:
The other answers just tell you how to make a list of the keys under the root group, which may refer to other groups or datasets.
If you want something closer to h5dump but in python, you can do something like that:
If you want to list the key names, you need to use the keys() method which gives you a key object, then use the list() method to list the keys: