diff --git a/rmgpy/data/base.py b/rmgpy/data/base.py index d2586a6b30..f166601708 100644 --- a/rmgpy/data/base.py +++ b/rmgpy/data/base.py @@ -236,13 +236,17 @@ def load(self, path, local_context=None, global_context=None): local_context[key] = value # Process the file - f = open(path, 'r') + with open(path, 'r') as f: + content = f.read() try: - exec(f.read(), global_context, local_context) - except Exception: - logging.error('Error while reading database {0!r}.'.format(path)) + exec(content, global_context, local_context) + except Exception as e: + logging.exception(f'Error while reading database file {path}.') + line_number = e.__traceback__.tb_next.tb_lineno + logging.error(f'Error occurred at or near line {line_number} of {path}.') + lines = content.splitlines() + logging.error(f'Line: {lines[line_number - 1]}') raise - f.close() # Extract the database metadata self.name = local_context['name']