How to read HDF5 attributes (metadata) with Python and h5py

Question:

I have a HDF5 file with multiple folders inside. Each folder has attributes added (some call attributes "metadata"). I know how to access the keys inside a folder, but I don’t know how to pull the attributes with Python’s h5py package.
Here are attributes from HDFView:

Folder1(800,4)
    Group size = 9
    Number of attributes = 1
        measRelTime_seconds = 201.73

I need to pull this measRelTime_seconds value. I already have a loop to read files:

f = h5py.File(file, 'r')
for k, key in enumerate(f.keys()): #loop over folders
    #need to obtain measRelTime_seconds here, I guess
Asked By: mrq

||

Answers:

Attributes work just like groups and datasets. Use object.attrs.keys() to iterate over the attribute names. The object could be a file, group or dataset.

Here is a simple example that creates 2 attributes on 3 different objects, then reads and prints them.

arr = np.random.randn(1000)

with h5py.File('groups.hdf5', 'w') as f:
    g = f.create_group('Base_Group')
    d = g.create_dataset('default', data=arr)

    f.attrs['User'] = 'Me'
    f.attrs['OS'] = 'Windows'

    g.attrs['Date'] = 'today'
    g.attrs['Time'] = 'now'

    d.attrs['attr1'] = 1.0
    d.attrs['attr2'] = 22.2
    
    for k in f.attrs.keys():
        print(f"{k} => {f.attrs[k]}")
    for k in g.attrs.keys():
        print(f"{k} => {g.attrs[k]}")
    for k in d.attrs.keys():
        print(f"{k} => {d.attrs[k]}")

    print('*****')
    
    for k in f.attrs.keys():
        print(f"{k} => {f.attrs[k]}")
    for k in f['Base_Group'].attrs.keys():
        print(f"{k} => {f['Base_Group'].attrs[k]}")
    for k in f['Base_Group']['default'].attrs.keys():
        print(f"{k} => {f['Base_Group']['default'].attrs[k]}")
Answered By: kcw78

Ok, I find my answer. To read it You can simply check the name of the attribute as

f['Folder'].attrs.keys()

and the value can be returned with

f['Folder'].attrs['<name of the attribute>']
Answered By: mrq