[Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

patrice laporte zepangolin at gmail.com
Mon Feb 15 21:32:07 CET 2010

2010/2/14 Steven D'Aprano <steve at pearwood.info>

> > If the file do esn't exist, bing ! I got an exception "I/O Error n°2
> > : file doesn't exist".
> Are you sure? What version of Python are you using? I get a completely
> different error message:
Yes I'm not sure .... when I wrote : *I got an exception "I/O Error n°2 :
file doesn't exist".*
It was just to stress the point, it was not the real message.

>>> open("no such file.txt", "r")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> IOError: [Errno 2] No such file or directory: 'no such file.txt'

 And of course,  If I do this, I got the same traceback, and I can see the
file name.

> >>> try:
> ...     open("no such file.txt", "r")
> ... except IOError, e:
> ...     pass
> ...
> >>> e.filename
> 'no such file.txt'
 This is it, this way to catch the exception what I wanted. Reading the
documentation and articles over the web, I found that :

   f = open("bling")
except IOError, (errno, errmes):
   # that way, I can only print the erreor number and the message
   # but the message doesn't contain the file name, and it confused me
because traceback can
   # give it. So where to find it ?

But with your example, I can access all attribute I can read doing >>*help(e)
*in the interpreter*
With documentation, I understood filename is an attribute, but I didn't
understand how to use it. The part of the documentation about 'buil-tins
Exception' and the other part about "how to handle exceptin" didn't help me,
there is something I missed about how to handle exception.

Thank you for this help, I will look closer again to exception.

Many thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100215/c2dc3c6c/attachment.htm>

More information about the Tutor mailing list