1

I want to copy whole .h5 file to new file due to library version issue in using MATLAB. But I have a problem: my .h5 data file contains some datasets and groups in root directory, and I tried to copy it using h5py:

originfile = h5py.File('file_0.raw.h5', 'r')
convertedfile = h5py.File('file_0.raw_conv.h5', 'w', libver=('earliest', 'v108'))

originfile.copy('/', convertedfile, name='/')

originfile.close()
convertedfile.close()

It doesn't work.. with error

RuntimeError: Unable to copy object (destination object already exists)

Can I copy in another way to avoid this error?

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
QORACLE
  • 11
  • 2
  • Does `file_0.raw_conv.h5` already exist? – possum Aug 10 '20 at 09:40
  • you could loop through all groups/datasets and copy the data by creating new datasets like f.create_dataset(data = oldfile['datasetname'], name = 'datasetname') – Stefan Aug 10 '20 at 11:17
  • Look at this answer: [Answer to SO 61525566](https://stackoverflow.com/a/61527336/10462884). It uses the `.copy()` method to recursively copy objects from f1.h5 to f2.h5. It reads the root level keys to access groups/datasets. If the source is a Group object, by default all objects within that group are copied recursively. – kcw78 Aug 10 '20 at 18:56
  • I solved this problem with [Answer to SO 61525566](https://stackoverflow.com/questions/61525566/is-is-possible-without-knowing-structure-to-make-hdf5-file-that-will-be-the-same/61527336#61527336). It works well. – QORACLE Aug 11 '20 at 01:22

0 Answers0