Reputation: 4968
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?
Upvotes: 2
Views: 2459
Reputation: 1207
Change h5py.File('test','w')
to h5py.File('test','a')
(or h5py.File('test')
, which defaults to the latter).
When you instantiate a h5py.File
object, you have to specify a mode
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-
or x
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.
Upvotes: 7