Skip to content Skip to sidebar Skip to footer

How To Partially Copy Using Python An Hdf5 File Into A New One Keeping The Same Structure?

I have a large hdf5 file that looks something like this: A/B/dataset1, dataset2 A/C/dataset1, dataset2 A/D/dataset1, dataset2 A/E/dataset1, dataset2 ... I want to create a new fil

Solution 1:

fs.copy('A/B', fd) doesn't copy the path /A/B/ into fd, it only copies the group B (as you've found out!). So you first need to create the rest of the path:

fd.create_group('A')
fs.copy('A/B', fd['/A'])

or, if you will be using the group a lot:

fd_A = fd.create_group('A')
fs.copy('A/B', fd_A)

This copies the group B from fs['/A/B'] into fd['/A']:

In [1]: fd['A/B'].keys()
Out[1]: [u'dataset1', u'dataset2']

Here's an automatic way of doing this:

# Get the name of the parent for the group we want to copy
group_path = fs['/A/B'].parent.name

# Check that this group exists in the destination file; if it doesn't, create it# This will create the parents too, if they don't exist
group_id = fd.require_group(group_path)

# Copy fs:/A/B/ to fd:/A/G
fs.copy('/A/B', group_id, name="G")

print(fd['/A/G'].keys())
# [u'dataset1', u'dataset2']

Post a Comment for "How To Partially Copy Using Python An Hdf5 File Into A New One Keeping The Same Structure?"