import tables
f = tables.open_file('my_file.hdf5', mode='w')
f.create_group(f.root, name='videos')
我还有另一个脚本可以将数据添加到“视频”组中。该脚本检查“视频”组是否已经存在;如果“视频”尚不存在,脚本将创建一个“视频”组。
如何检查组是否已经存在?我尝试使用f.walk_groups()和f.root._v_groups,但这些似乎不是最佳解决方案。以及如何获取包含f.root中所有组的名称(作为字符串)的列表?
您需要做的就是检查根组中是否存在组/数据集路径名。这将返回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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句