h5py报错:数据集与组名冲突及文件读写异常求助
HDF5文件操作报错排查
遇到两类HDF5操作报错,具体情况如下:
第一类报错场景
执行代码时出现两种矛盾报错:
- 程序检测不到文件中已存在的路径,尝试创建新数据集时抛出
TypeError: "Incompatible object (Dataset) already exists" - 若跳过检测直接用
dset = file[hdf5_path]读取数据集更新值,会报错Unable to open object (message type not found)
报错代码:
hdf5_path = "path/to/my/dataset" with h5py.File(hdf5_path, "r+") as file: if hdf5_path in file: dset = file[hdf5_path] dset[...] = new_data_set else: file.create_dataset(hdf5_path, data=pixel_count)
第二类报错场景
执行代码时,循环第二次创建组my_path/to_another时,报错Unable to create group (message type not found)
报错代码:
import h5py data_set = [1,2,3,4] fname = "temp.h5" h5_path = "my_path" with h5py.File(fname, "r+") as file: if h5_path in file: dset = file[h5_path] dset[...] = data_set else: file.create_dataset(h5_path, data=data_set) h5_path = "my_path/to_another/dest" with h5py.File(fname, "r+") as file: current_path = "" for part in h5_path.split('/')[:-1]: current_path = f"{current_path}/{part}" if current_path else part if current_path not in file: file.create_group(current_path) if h5_path in file: dset = file[h5_path] dset[...] = data_set else: file.create_dataset(h5_path, data=data_set)
请问这些问题是文件损坏导致的吗?
内容的提问来源于stack exchange,提问作者hamo
相关产品推荐
相关产品推荐

