Is it possible to create a HDF5 structure matching the file path on a local pc?

Question:

I am trying to automatically create a HDF5 structure by using the file paths on my local pc. I want to read through the subdirectories and create a HDF5 structure to match, that I can then save files to. Thank you

Asked By: Andrew Little

||

Answers:

This is actually quite easy to do using HDFql in Python. Here is a complete script that does that:

# import HDFql module (make sure it can be found by the Python interpreter)
import HDFql

# create and use (i.e. open) an HDF5 file named 'my_file.h5'
HDFql.execute("CREATE AND USE FILE my_file.h5")

# get all directories recursively starting from the root of the file system (/)
HDFql.execute("SHOW DIRECTORY / LIKE **")

# iterate result set and create group in each iteration
while HDFql.cursor_next() == HDFql.SUCCESS:
   HDFql.execute("CREATE GROUP "%s"" % HDFql.cursor_get_char())
Answered By: SOG

You can do this by combining os.walk() and h5py create_group(). The only complications are handling Linux vs Windows (and drive letter on Windows). Another consideration is relative vs absolute path. (I used absolute path, but my example can be modified. (Note: it’s a little verbose so you can see what’s going on.) Here is the example code (for Windows):

with h5py.File('SO_73879694.h5','w') as h5f:
    cwd = os.getcwd()
    for root, dirs, _ in os.walk(cwd, topdown=True):
        print(f'ROOT: {root}')
        # for Windows, modify root: remove drive letter and replace backslashes:
        grp_name = root[2:].replace( '\', '/')
        print(f'grp_name: {grp_name}n')
        h5f.create_group(grp_name)
Answered By: kcw78
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.