Throw an error if a folder is empty
python-3.x
Solution
You can use `sys.exit('message')` to achieve a cleaner error message.
import sys
panosDir = '.src/panos'
if not os.listdir(panosDir):
sys.exit('Panos directory is empty')
See this for more information. It states the following:
In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
Problem
I'm writing a script, and I'd like to stop the script if a folder is empty. This is what I have so far: ``` panosDir = '.src/panos' if not os.listdir(panosDir): raise Exception('Panos directory is empty') ``` The problem I have with this code is that I consider the warning in the terminal a bit too much: ``` Traceback (most recent call last): File "E:\virtual_tours\.archives\bin\python\makeTiles.py", line 32, in <module> if __name__ == '__main__': main() File "E:\virtual_tours\.archives\bin\python\makeTiles.py", line 23, in main raise Exception('Panos directory is empty') Exception: Panos directory is empty ``` Is there a better way in Python to stop executing a script and throw a customized message?