You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 04:20:58