2010/2/14 Steven D'Aprano <[email protected]>
>
> > 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 :
try:
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
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor