All you need to do is create a global variable to hold the data.
See example below. It creates a file with some groups and datasets, then calls visititems()
. It creates and returns a dictionary of names and object types. I prefer visititems()
over visit()
so I can reference the h5 node as an object. You should be able to use this as a template for your process.
def visit_all_nodes(name, node):
global node_dict
node_dict = {}
fullname = node.name
if isinstance(node, h5py.Dataset):
# node is a dataset
print(f'adding Dataset: {fullname}')
node_dict[fullname] = 'Dataset'
elif isinstance(node, h5py.Group):
# node is a group
print(f'adding Group: {fullname}')
node_dict[fullname] = 'Group'
else:
# node type not dataset or group
print(f'Node: {fullname}; skipping')
with h5py.File('File.h5','w') as h5f:
h5f.create_group('Group')
h5f.create_group('Group/subGroup')
h5f.create_dataset('Group/subGroup/NotaName',shape=(10,10))
h5f.create_group('Group2')
h5f.create_group('Group2/subGroup')
h5f.create_dataset('Group2/subGroup/Name',shape=(10,10))
print ('**Walking file to get all nodes**\n')
h5f.visititems(visit_all_nodes)
print()
for k,v in node_dict.items():
print(f'Node name: {k}, Type: {v}')