您需要做的就是检查根组中是否存在组/数据集路径名。这将返回 True/False:'/videos' in f.root
所以你可以像这样创建一个if 块:
if '/videos' in f.root:
do something
这是一个简短的代码段,它创建一个组 + 2 个数组数据集,然后打印组和 2 个数据集的逻辑测试。请注意,arr3 不存在。
import tables as tb
import numpy as np
with tb.File('SO_65327481.hdf5', mode='w') as h5f:
h5f.create_group('/', name='videos')
arr = np.random.random((10,10))
h5f.create_array('/videos',name='arr1', obj=arr)
arr = np.random.random((10,10))
h5f.create_array('/videos','arr2',obj=arr)
print ( 'videos exists:', '/videos' in h5f.root)
print ( 'arr2 exists:', '/videos/arr2' in h5f.root)
print ( 'arr3 exists:', '/videos/arr3' in h5f.root)
输出将是:
videos exists: True
arr2 exists: True
arr3 exists: False