Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python exception message capturing

import ftplib import urllib2 import os import logging logger = logging.getLogger('ftpuploader') hdlr = logging.FileHandler('ftplog.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.INFO) FTPADDR = "some ftp address"  def upload_to_ftp(con, filepath):     try:         f = open(filepath,'rb')                # file to send         con.storbinary('STOR '+ filepath, f)         # Send the file         f.close()                                # Close file and FTP         logger.info('File successfully uploaded to '+ FTPADDR)     except, e:         logger.error('Failed to upload to ftp: '+ str(e)) 

This doesn't seem to work, I get syntax error, what is the proper way of doing this for logging all kind of exceptions to a file

like image 328
Hellnar Avatar asked Jan 14 '11 11:01

Hellnar


People also ask

How do I capture an exception message in Python?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.

How do I catch an exception and print message?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How can I get error message from exception object?

Description. msgText = getReport( exception ) gets the error message for an exception and returns it as formatted text, msgText . The message is the value of the message property of the MException object, exception . It is the same text that MATLAB® displays when it throws the exception.

Does catching an exception stop execution Python?

However, if an exception is raised in the try clause, Python will stop executing any more code in that clause, and pass the exception to the except clause to see if this particular error is handled there.


1 Answers

You have to define which type of exception you want to catch. So write except Exception, e: instead of except, e: for a general exception (that will be logged anyway).

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, e: # work on python 2.x     logger.error('Failed to upload to ftp: '+ str(e)) 

in Python 3.x and modern versions of Python 2.x use except Exception as e instead of except Exception, e:

try:     with open(filepath,'rb') as f:         con.storbinary('STOR '+ filepath, f)     logger.info('File successfully uploaded to '+ FTPADDR) except Exception as e: # work on python 3.x     logger.error('Failed to upload to ftp: '+ str(e)) 
like image 115
eumiro Avatar answered Sep 22 '22 08:09

eumiro