import h5py
import numpy as np
f = h5py.File('test','w')
f.create_dataset('key1', data = np.array([1,2,3]))
f.create_dataset('key2', data = np.array([4,5,6]))
f.close()
creates the file named test and writes two arrays under key1 and key2 respectively.
However, closing the file object and reopening the file deletes the data previously stored.
f = h5py.File('test','w')
f.create_dataset('key1', data = np.array([1,2,3]))
f.close()
f = h5py.File('test','w')
f.create_dataset('key2', data = np.array([4,5,6]))
f.close()
In this case only [4,5,6] is stored under the key key2.
How to reopen the file and write new data without deleting the old data which is already stored?
Quick answer
Change
h5py.File('test','w')toh5py.File('test','a')(orh5py.File('test'), which defaults to the latter).Why
When you instantiate a
h5py.Fileobject, you have to specify amodeas the second parameter. This must be one of the following:rReadonly, file must existr+Read/write, file must existwCreate file, truncate if existsw-orxCreate file, fail if existsaRead/write if exists, create otherwise (default)Using
ais a quick fix, but risky if your program doesn't always know whether the file already exists. You can achieve any desired behavior in a less ambiguous way by using the other modes along with file checking.