.h5 文件格式无法正确关闭

数据挖掘 Python 数据格式
2022-03-05 21:21:53
import h5py #added
hf = h5py.File('../images.h5', 'w') #added
hf.close() #added

h5_file = tables.open_file("images.h5", mode="w")

我也试过:

h5py.File.close(hf)

在这两种情况下弹出的错误是:

ValueError: The file 'restricted_images.h5' is already opened.  Please close it before reopening in write mode.

我也试过:

if isinstance(obj, h5py.File):   # Just HDF5 files
    obj.close()

尽管

In[]: hf
Out[]: <Closed HDF5 file> 

,文件尚未关闭。

1个回答

您可能想要使用类似以下代码段的内容:

with h5py.File("some_path.h5") as f:
   f["data1"] = some_data

import h5py #added
with h5py.File('../images.h5', 'w') as f:    
    h5_file = tables.open_file("images.h5", mode="w")
    # do what ever you want to do, it will be closed by itself.