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.File
object, you have to specify amode
as the second parameter. This must be one of the following:r
Readonly, file must existr+
Read/write, file must existw
Create file, truncate if existsw-
orx
Create file, fail if existsa
Read/write if exists, create otherwise (default)Using
a
is 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.