HDF5 file created with h5py can't be opened by h5py

后端 未结 2 1246
长情又很酷
长情又很酷 2020-12-09 17:01

I created an HDF5 file apparently without any problems, under Ubuntu 12.04 (32bit version), using Anaconda as Python distribution and writing in ipython notebooks. The under

2条回答
  •  执笔经年
    2020-12-09 17:46

    Since we resolved the issue in the comments on my question, I'm writing the results out here to mark it as solved.

    The main problem was that I forgot to close the file after I created it. There would have been two simple options, either:

    import numpy as np
    import h5py
    
    f = h5py.File('myfile.hdf5','w')
    group = f.create_group('a_group')
    group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
    f.close()
    

    or, my favourite because the file is closed automatically:

    import numpy as np
    import h5py
    
    with h5py.File('myfile.hdf5','w') as f:
        group = f.create_group('a_group')
        group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
    

提交回复
热议问题