H5py: reopening file in write mode deletes previous data

2.3k views Asked by At
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?

1

There are 1 answers

0
rjonnal On BEST ANSWER

Quick answer

Change h5py.File('test','w') to h5py.File('test','a') (or h5py.File('test'), which defaults to the latter).

Why

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 exist
  • r+ Read/write, file must exist
  • w Create file, truncate if exists
  • w- or x Create file, fail if exists
  • a 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.