You might want to use the visititems method.
Recursively visit all objects in this group and subgroups. Like Group.visit(), except your callable should have the signature:
callable(name, object) -> None or return value
.
In this case object will be a Group or Dataset instance.
So the idea is to have a function that will take as argument the name of the visited group (or dataset) and the group (or dataset) instance to log and call the visititems
function of the opened file with this log function as argument.
Here is a simple example implementation:
def log_hdf_file(hdf_file):
"""
Print the groups, attributes and datasets contained in the given HDF file handler to stdout.
:param h5py.File hdf_file: HDF file handler to log to stdout.
"""
def _print_item(name, item):
"""Print to stdout the name and attributes or value of the visited item."""
print name
# Format item attributes if any
if item.attrs:
print '\tattributes:'
for key, value in item.attrs.iteritems():
print '\t\t{}: {}'.format(key, str(value).replace('\n', '\n\t\t'))
# Format Dataset value
if hasattr(item, 'value'):
print '\tValue:'
print '\t\t' + str(item.value).replace('\n', '\n\t\t')
# Here we first print the file attributes as they are not accessible from File.visititems()
_print_item(hdf_file.filename, hdf_file)
# Print the content of the file
hdf_file.visititems(_print_item)
with h5py.File('my_file.h5') as hdf_file:
log_hdf_file(hdf_file)