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
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