Arij SEDIRI
Arij SEDIRI

Reputation: 2158

Python Exceptions Handling: Exception didn't work

My code downloads files from an online file storage web service and unzip them. To run my script, I give it a date as argument (as mentioned here below). I want to add an error message if the date did not match any documents.

So assuming that I have one month of data (July), a valid date is an existing folder which contains information about the date in question (example: 07Jul2016). Obviously, a wrong date is an unexisting folder in my online file storage web service (example: 18Aug2016).

This is what I'm supposed to do in my main function:

download_usrm(date = sys.argv[1]) 

And obviously, this is my command line to run my script:

python Extractor.py 07Jul2016

This is the code in question (This is just the try/except part):

if key_element.startswith(date):

    try:                
        if not os.path.exists(working_directory+ '/' +date_file+ '/'+parentKey):
            os.makedirs(working_directory+ '/' +date_file+ '/'+parentKey)
        key.get_contents_to_filename(working_directory+ '/' +date_file+ '/'+keyString)

       for f in glob.glob(working_directory+ '/' + date_file+ '/'+ 'usrm' + '/'+ file_name+ '*.zip'):
           zip_ref = zipfile.ZipFile(f, 'a')
           zip_ref.extractall(working_directory + '/' + date_file+ '/'+ 'usrm' + '/' + file_id)
           zip_ref.close()
           os.remove(f)

    except Exception as e:
        print('ERROR MESSAGE : Your search did not match any documents. Try running with another date.', e)

So with a valid date, my code is normally running. With a wrong date, I have nothing. The error message is not displayed.

What might be wrong? Any help appreciated. Thanks!

Upvotes: 0

Views: 57

Answers (0)

Related Questions