Search code examples
python-3.xpytables

NoSuchNodeError when determine tables in file


I use PyTables, I want to check whether a table has been created or not, if not, then create it.

I use the following code:

if handle.__contains__(handle.root.grades)==False:
    handle.create_table('/', 'grades', grades)

while when there is no such table named "grades", the program report error:"NoSuchNodeError: group / does not have a child named grades"

once there is a table named "grades", the following

handle.__contains__(handle.root.grades)

returns True.

How should I determine whether there is certain table?


Solution

  • I use the following expression to deal with the problem.

    import tables as tb
    try:
       handle.__contains__(handle.root.grades)
       print('contains grades')
    except tb.NoSuchNodeError:
       print('there is no grades, will create it')
       handle.create_table('/', 'grades', grades)
    

    and in Pytables, the expression for file.contains is

    def __contains__(self, path):
    
        try:
            self.get_node(path)
        except NoSuchNodeError:
            return False
        else:
            return True
    

    so, does pytables have some problems? or I don't import tables correctly?