import traceback try: exec(' open(filepath)') except Exception as exc: traceback.print_exc() raise # ** Output ** # # Traceback (most recent call last): # File "test_indenterror.py", line 5, in # exec(' open(filepath)') # File "", line 1 # open(filepath) # ^ # IndentationError: unexpected indent # Traceback (most recent call last): # File "test_indenterror.py", line 5, in # exec(' open(filepath)') # File "", line 1 # open(filepath) # ^ # IndentationError: unexpected indent