You have to define which type of exception you want to catch. So write except Exception as e: instead of except, e: for a general exception.
Other possibility is to write your whole try/except code this way:
try:
with open(filepath,'rb') as f:
con.storbinary('STOR '+ filepath, f)
logger.info('File successfully uploaded to '+ FTPADDR)
except Exception as e: # works on python 3.x
logger.error('Failed to upload to ftp: %s', repr(e))
In older versions of Python 2.x, use except Exception, e instead of except Exception as e:
try:
with open(filepath,'rb') as f:
con.storbinary('STOR '+ filepath, f)
logger.info('File successfully uploaded to %s', FTPADDR)
except Exception, e: # works on python 2.x
logger.error('Failed to upload to ftp: %s', repr(e))
Answer from eumiro on Stack OverflowYou have to define which type of exception you want to catch. So write except Exception as e: instead of except, e: for a general exception.
Other possibility is to write your whole try/except code this way:
try:
with open(filepath,'rb') as f:
con.storbinary('STOR '+ filepath, f)
logger.info('File successfully uploaded to '+ FTPADDR)
except Exception as e: # works on python 3.x
logger.error('Failed to upload to ftp: %s', repr(e))
In older versions of Python 2.x, use except Exception, e instead of except Exception as e:
try:
with open(filepath,'rb') as f:
con.storbinary('STOR '+ filepath, f)
logger.info('File successfully uploaded to %s', FTPADDR)
except Exception, e: # works on python 2.x
logger.error('Failed to upload to ftp: %s', repr(e))
The syntax except, e: is no longer supported in python 3. Use the following instead.
try:
do_something()
except BaseException as e:
logger.error('Failed to do something: %s', str(e))
How would I get my error message to print before Python errors?
Python Exception Handling: Getting the error message
Capturing Full Exception Message?
Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
I am trying to make a pay calculator to calculate overtime, and my calculator is set up like this:
https://hastebin.com/wehuroruzi.py
From line 14 to 19 I state that If the 'pay_rate' input from the user isn't an integer or float point, then send an error message saying that the user must supply a number.
My issue is that Python gives me an error before the message can be sent to the user. The error being that whatever string that the user sent has no definition in the code.
So I'm having a script that is running on a schedule from a virtual machine at odd times of the day. Have a weird issue with it that when I run it manually it works perfectly fine, but it breaks due to KeyError when it runs automatically.
It's closely tied to the time zones and processing data from various time zones and bringing them in sync, so it's not unexpected that I have issue on it appearing when it runs at midnight (in relevant time zones).
However, I need to see which data source is breaking exactly and I'm having trouble doing so. I attempted to capture the error using try/except and write it to a txt file in this way:
try:
# code lives here
except Exception as e
#opening the file
f.write(str(e))
However, this only logs the key that wasn't found and as luck would have it, it means I have a minimum of three places to look for that. Would be extremely helpful if I could somehow also capture the full error message I would get in the terminal (with info on which line caused the issue and similar).
Thanks in advance!