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))
这似乎不起作用,我得到了语法错误,记录所有类型的异常到文件的正确方法是什么?
except
后面省略逗号。 - Sven Marnachexcept
之后省略逗号,你会得到global name 'e' is not defined
的错误提示,这并没有比语法错误好多少。请注意不要改变原意。 - Valexcept Exception as e
或except Exception, e
。 - Sven Marnach