If I have one hdf5 file f1.h5
and I want to make a copy of this file to another one (e.g. f2.h5
), but I don't know the structure of f1.h5
and I want to copy it automatically, can I do it with some tips of h5py
?
Asked
Active
Viewed 1,014 times
-1

kitsune_breeze
- 97
- 1
- 11
2 Answers
3
You can use the .copy()
method to recursively copy objects from f1.h5
to f2.h5
. You don't need to know the schema: use keys to access groups/datasets at the root level. If the source is a Group object, by default all objects within that group will be copied recursively.
import h5py
h5r=h5py.File("f1.h5", 'r')
with h5py.File("f2.h5", 'w') as h5w:
for obj in h5r.keys():
h5r.copy(obj, h5w )
h5r.close()

kcw78
- 7,131
- 3
- 12
- 44
-
There is a similar answer here: [How to copy a dataset object with h5py](https://stackoverflow.com/a/53462953/10462884) – kcw78 Apr 30 '20 at 15:45
1
I don't know about h5py, but it should be possible by:
f1=open('f1.h5','rb')
f2=open('f2.h5','wb')
f2.write(f1.read())
You read each byte of the first file and write it to the second file. Things such as structure don't matter

b2f
- 196
- 2
- 10
-
I am not sure that it is possible to use with files that have fields to acces to daaset. But I'll try this, thanx – kitsune_breeze Apr 30 '20 at 15:04
-
@b2f, will this work for any size file? Frequently HDF5 files are larger than system RAM, so users work with subsets of data. – kcw78 May 01 '20 at 14:01