Fine-grained OSError subclasses or cross-platform errnos?

J

John J. Lee

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top