Python Fiona – Error Handling When Opening Shapefile with Fiona

file pathfionapython

I've written a python script where I read a bunch of files trying to catch various errors (FileNotFound, Import), for example

try:
    df = pd.read_excel(os.path.join(path_to_data, fname_data))
except OSError as e:
    sys.exit(e)
except ImportError as e:
    sys.exit(e)

and mostly get the expected kind of error messages such as

SystemExit: [Errno 2] No such file or directory: '/media/user/data.ods'

However, trying to read a shapefile with fiona I get an annoyingly long error message, even though this is definitely just FileNotFound; if the file exists the code works just fine.

Can someone explain the reason for this long error message from fiona and how to properly catch this exception?

try:
    with fiona.open(os.path.join(path_to_data, fname_shp), "r") as shapefile:
        features = [feature for feature in shapefile]
except OSError as error:
    sys.exit(error)
Traceback (most recent call last):

  File "fiona/_shim.pyx", line 83, in fiona._shim.gdal_open_vector

  File "fiona/_err.pyx", line 291, in fiona._err.exc_wrap_pointer

CPLE_OpenFailedError: /media/user/poly.shp: No such file or directory


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/home/user/jupyter/Project_Twide.py", line 525, in <module>
    read_input(path_to_data, fname_dem)

  File "/home/user/jupyter/Project_Twide.py", line 104, in read_input
    with fiona.open(os.path.join(path_to_data, fname_shp), "r") as shapefile:

  File "/home/user/miniconda3/envs/py39/lib/python3.9/site-packages/fiona/env.py", line 417, in wrapper
    return f(*args, **kwargs)

  File "/home/user/miniconda3/envs/py39/lib/python3.9/site-packages/fiona/__init__.py", line 264, in open
    c = Collection(path, mode, driver=driver, encoding=encoding,

  File "/home/user/miniconda3/envs/py39/lib/python3.9/site-packages/fiona/collection.py", line 162, in __init__
    self.session.start(self, **kwargs)

  File "fiona/ogrext.pyx", line 540, in fiona.ogrext.Session.start

  File "fiona/_shim.pyx", line 90, in fiona._shim.gdal_open_vector

DriverError: /media/user/poly.shp: No such file or directory

Best Answer

I see a slightly different message which is enlightening:

...
  File "fiona/ogrext.pyx", line 540, in fiona.ogrext.Session.start
  File "fiona/_shim.pyx", line 81, in fiona._shim.gdal_open_vector
fiona.errors.DriverError: ./bar: No such file or directory

This is telling me the exception is fiona.errors.DriverError. (I don't know why you only see DriverError, maybe something else is imported...) The fiona module has a bunch of exception handlers (see help(fiona.errors)) to catch stuff from the underlying code.

So, you can trap this one with:

try:
    with fiona.open(os.path.join(path_to_data, fname_shp), "r") as shapefile:
        features = [feature for feature in shapefile]
except OSError as error:
    sys.exit(error)
except fiona.errors.DriverError:
    print("fiona error")

Then my test script does:

$ python err.py foo bar
fiona error

The reason for the complication is that an error in opening a GDAL/OGR data set might not be a simple "file not found" - its going to be something like "layer not found in DSN" or "DSN not found" since GDAL/OGR sees data sources as layers in DSNs, where DSNs could be databases, or zip files, or directories. The underlying error seems related to GDAL/OGR's CPLE_OpenFailedError via the fiona._err.CPLE_OpenFailedError but that's not meant for us users (see help(fiona._err.CPLE_OpenFailedError))

Related Question