Search code examples
pythonhdf5pytables

Create HDF5 Group /Table if it does not exist


I am building an HDF5 file using PyTables python package. The file would be updated everyday with latest tick data. I want to create two groups - Quotes and Trades and tables for different futures expiries. I want to check if the group Quotes exists or not and if not then create it. What is the best way to do it in PyTables?

Here is a code snippet of where I am right now:

hdf_repos_filters = tables.Filters(complevel=1, complib='zlib')
for instrument in instruments:
    if options.verbose:
    hdf_file = os.path.join(dest_path, "{}.h5".format(instrument))
    store = tables.open_file(hdf_file, mode='a', filters=hdf_repos_filters)
    # This is where I want to check whether the group "Quotes" and "Trades" exist and if not create it

Solution

  • I think I have figured it out.

    I am using the File.__contains__(path) method in the File class in PyTables.

    As per the documentation:

    File.__contains__(path)
    

    Is there a node with that path?

    Returns True if the file has a node with the given path (a string), False otherwise.

    PyTables File class