How to write hdf5 files without overwriting?

Sorry if this is a very simple question on h5py.

I read the documentation, but I did not find a similar example.

I am trying to create several hdf5 datasets in Python, but it turns out that after closing the file, the data will be overwritten.

Let's say I do the following:

import numpy as np import h5py f = h5py.File('test.hdf5', 'w') f.create_dataset('data1', data = np.ones(10)) f.close() f = h5py.File('test.hdf5', 'w') f.create_dataset('data0', data = np.zeros(10)) f.close() f = h5py.File('test.hdf5', 'r') f["data1"].value f.close() 

I get

KeyError: "Unable to open object (Object 'data1' does not exist)

If I add data, this requires first opening in 'w' mode, and then adding in 'a' mode, having two different statements.

 import numpy as np import h5py f = h5py.File('test.hdf5', 'w') f.create_dataset('data1', data = np.ones(10)) f.close() f = h5py.File('test.hdf5', 'a') f.create_dataset('data0', data = np.zeros(10)) f.close() f = h5py.File('test.hdf5', 'r') f["data1"].value f.close() 

If I open the file in 'a' mode in both cases:

 import numpy as np import h5py f = h5py.File('test.hdf5', 'a') f.create_dataset('data1', data = np.ones(10)) f.close() f = h5py.File('test.hdf5', 'a') f.create_dataset('data0', data = np.zeros(10)) f.close() f = h5py.File('test.hdf5', 'r') print(f['data1'].value) f.close() 

RuntimeError: unable to create link (name already exists)

According to the documentation, the data should be stored contiguously, but I did not find how to avoid overwriting the data.

How to store data on previously closed hdf5 with just one operator?

+4
source share
1 answer

If you want to create a unique file in each run, you should consider naming such a file, for example, adding a timestamp to the file name. A very simple example is to use datetime and now and strftime to create a file name. Example -

 import datetime filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")) 

Then you can use this file name to open the file.


Demo -

 >>> import datetime >>> filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")) >>> filename 'test_2015_08_09_13_33_43.hdf5' 
+2
source

All Articles