[Tutor] IOError exception handling

Sean 'Shaleh' Perry shalehperry@attbi.com
Sun, 28 Jul 2002 16:53:49 -0700 (PDT)


On 28-Jul-2002 Allyn Weaks wrote:
> Python 2.1 until I can get 2.2.1 installed properly (linux 7.1), but
> I'll also be using 2.0 on another (aix) server.
> 
> Can one get a more detailed error back than IOError?  I'm counting
> lines in files that may or may not exist, and if one doesn't exist, I
> can set the number of lines to zero and continue with the rest.  But,
> if the file can't be opened because of any other problem, such as a
> permission error, I want it to bail with whatever error message is
> appropriate (preferably without handling each and every case.)
> 

>>> fd = open('/home/shoon/xinitrc')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 13] Permission denied: '/home/shoon/xinitrc'

As you can see, IOError covers many issues

>>> try:
...   fd = open('/home/shoon/xinitrc')
... except IOError, e:
...   print e[0], e[1]
... 
13 Permission denied

I am sure there is a wrapper for errno somewhere in python.