Maybe overkill, but I had this and might be useful for someone:
from __future__ import print_function
def scan_hdf5(path, recursive=True, tab_step=2):
def scan_node(g, tabs=0):
print(' ' * tabs, g.name)
for k, v in g.items():
if isinstance(v, h5.Dataset):
print(' ' * tabs + ' ' * tab_step + ' -', v.name)
elif isinstance(v, h5.Group) and recursive:
scan_node(v, tabs=tabs + tab_step)
with h5.File(path, 'r') as f:
scan_node(f)
And simple input:
>>> scan_hdf5('/tmp/dummy.h5')
/
- /d1
/g1
- /g1/d2
- /g1/d3
/g2
- /g2/d4
/g2/g3
- /g2/g3/d5
Or an alternative version that returns the elements in something more usable:
def scan_hdf52(path, recursive=True, tab_step=2):
def scan_node(g, tabs=0):
elems = []
for k, v in g.items():
if isinstance(v, h5.Dataset):
elems.append(v.name)
elif isinstance(v, h5.Group) and recursive:
elems.append((v.name, scan_node(v, tabs=tabs + tab_step)))
return elems
with h5.File(path, 'r') as f:
return scan_node(f)
with returns:
>>> scan_hdf5_2('/tmp/dummy.h5')
[u'/d1',
(u'/g1', [u'/g1/d2', u'/g1/d3']),
(u'/g2', [u'/g2/d4', (u'/g2/g3', [u'/g2/g3/d5'])])]