sys.exc_info()[1] Python 2.71 中的类型和格式 [英] sys.exc_info()[1] type and format in Python 2.71

查看:37
本文介绍了sys.exc_info()[1] Python 2.71 中的类型和格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows XP 上的 python 2.71 中,我需要使用 FTP.我的代码是:

In python 2.71 on Windows XP I need to use FTP. My code is :

try:
    ftp = FTP(trec.address)
    ftp.login(trec.login, trec.passw)
    s = ftp.retrlines('LIST ' + trec.filetype)
    ftp.quit()
except:
    (type, value, tb) = sys.exc_info()
    reponse = "%s" % value

但我在最后一行有错误:UnicodeDecodeError:ascii"编解码器无法解码位置 38 中的字节 0xea:序号不在范围内 (128)
因为我在法语 Windows 环境中.sys.exc_info()[1] 是:[Errno 10061] Aucune connexion n'a pu être établie car l'ordinateur cible l'a expressément refusée
格式化 sys.exc_info()[1] 的最有效方法是什么?

But I have an error on the last line : UnicodeDecodeError: 'ascii' codec can't decode byte 0xea in position 38: ordinal not in range(128)
As I am in French Windows env. the sys.exc_info()[1] is : [Errno 10061] Aucune connexion n'a pu être établie car l'ordinateur cible l'a expressément refusée
What is the most efficient way to format sys.exc_info()[1] ?

推荐答案

好的,我发现最好的方法是像这样使用回溯:

OK, the best way I found is to use traceback like this :

import traceback

def trace_except(sysexecinfo, smessage = ''):
   """ Trace exceptions """
   exc_type, exc_value, exc_traceback = sysexecinfo
   i, j = (traceback.extract_tb(exc_traceback, 1))[0][0:2]
   k = (traceback.format_exception_only(exc_type, exc_value))[0]
   trace('E:'+ 'Err : ' + smessage + k + i + ', ligne ' + str(j))
   return k

 try:
   ftp = FTP(trec.address)
   ftp.login(trec.login, trec.passw)
   s = ftp.retrlines('LIST ' + trec.filetype)
   ftp.quit() 
 except:
    reponse = trace_except(sys.exc_info())

这篇关于sys.exc_info()[1] Python 2.71 中的类型和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆