2
votes

I have a hdf5 file I want to modify by deleting an attribute of one of the datasets and save the file without further changes. I can do this in hdfview, but I need something that's scriptable because it needs to be applied to a large number of files.

I tried writing a script in python, using h5py:

import h5py
inF = h5py.File("Filename.h5", 'r')
dSet = inF['/data/myDataset']
del dSet.attrs['myAttrName']

But I get the following error:

Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/h5py/_hl/attrs.py", line 75, in delitem h5a.delete(self._id, self._e(name)) File "h5a.pyx", line 135, in h5py.h5a.delete (h5py/h5a.c:2682) KeyError: "unable to delete attribute (Attribute: Can't delete message)"

print dSet.attrs['myAttrName'] produces the correct value, proving I can access the attribute.

Is there any other ways to do this? Maybe using h5repack?

2

2 Answers

3
votes

The problem with the h5py script is that you've opened the file in read-only mode ("r"). You need to open with append, like so:

inF = h5py.File("Filename.h5", 'a')
1
votes

I ended up using one of the hdf tools - h5copy:

h5copy -p -i inputFile.h5 -o outputFile.h5 -s /inputDataSetName -d /outputDataSetName -f noattr

Since all of the files I have to process have the same datasets and it's fairly small number of dataset,s I wrote a shell script that calls h5copy on each.

Note that running h5copy on the a group will not remove the attributes of all datasets in that group.