Fine-grained OSError subclasses or cross-platform errnos?

John J. Lee jjl at pobox.com
Mon Aug 2 15:34:22 EDT 2004


Today, I wanted to do something like this:

def nr_files(dirName)
    try:
        return len(os.listdir(dirName))
    except OSError, e:
        if e.errno == errno.ENOENT:
            return 0
        else:
            raise

In other words, I wanted to trap only the case of missing directory
(and not, for example, insufficient permissions), and I wanted to do
it in the EAFP style.

The problem is that errno is not cross-platform.  Neither is there any
subclass of OSError that I can conveniently catch to figure out what
went wrong.

Is there any better way of doing this?

If not, why not?


John



More information about the Python-list mailing list