Avoid seeing error message twice when raising error in python

Viewed 115

I would like my python program to raise an error:

import os

if not os.path.isdir(args.sample_dir):
    raise OSError('Samples not found.')

When running the code, I receive the following error when the above requirement is met:

Traceback (most recent call last):
  File "/Users/blade/utils.py", line 696, in <module>
    main()
  File "/Users/blade/utils.py", line 663, in main
    raise OSError('Samples not found.')
OSError: Samples not found.

I don't like that I see the error sentence twice. Is there a way to hide the earlier one from log?

1 Answers

I suggest you can use try/except for ignoring one of the error. try: os.path.isdir(args.sample_dir) except
raise OSError('Samples not found.')

Related