[Tutor] errno module - was File copying - best way?

fleet@teachout.org fleet@teachout.org
Sun, 5 Aug 2001 18:42:42 -0400 (EDT)


On Sun, 5 Aug 2001, Michael P. Reilly wrote:

> On Sun, Aug 05, 2001 at 01:06:09PM -0400, fleet@teachout.org wrote:
> >
> > I will study this and look at the rest of the exceptions listed for 'os;'
> > but what about the 'errno' module?

[good examples clipped - see previous message]
>
> Also notice that the filename is included.  Not on all, but the ones that
> relate to file errors, will have this attribute.

That could be handy.

> Since a lot of different possible values can come from the exception,
> sometimes it is valuable to search for the proper error in the except
> clause:
>
> try:
>   dbfile = open(dbfilename, 'rb+')
> except IOError, err:
>   if err.errno == errno.EACCES:
>     dbfilename = find_alternate_location(dbfilename)
>     dbfile = open(dbfilename, 'wb')
>
>   elif err.errno == errno.ENOENT:
>     dbfile = open(dbfilename, 'wb+')
>     initialize_database(dbfile)

Would this be a good candidate for a Class - ie FindError()

try:
  dbfile = open(dbfilename, 'rb+')
except IOError:
  FindError()

Something like that?

> The only other purpose I have found for using the errno module is when
> you are raising your own IOError or OSError exceptions.

Ok.  But errno module won't work with the os.popen() function because it
(popen) doesn't raise an exception if it fails?  Am I close?

				- fleet -