Its very possible its ok due now due to changes you made.

The constant CERT_PATH_NAME was not properly defined before (commons.media_path was undefined when running as a python script) this line of code ran and os.listdir threw an exception.