Toblerity / Fiona

Fiona reads and writes geographic data files
https://fiona.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.16k stars 202 forks source link

Add methods _dir_exists and _isdir #1436

Closed adriantre closed 2 months ago

adriantre commented 2 months ago

This PR is my best effort to add utility methods for checking if a file or vsi exists, and if it is a directory. (I have no expereince with Pyrex and C, so I would love feedback and guidance.)

The motivation comes from the need of these checks to list a directory recursively. The current workaround is this, which is a bit hacky.

try:
    subdirs = fiona.listdir(dir)
except FionaValueError as e:
    if 'does not exist' in str(e):
        # handle does not exist
    elif 'is not a directory' in str(e):
        # handle is a file
    else:
        # handle is a directory

with this pr, this would instead be possible:

if not fiona.dir_exists(path):
    # handle does not exist
elif fiona.isdir(path):
    # handle is a directory
else:
    # handle is a file
sgillies commented 2 months ago

@adriantre I'm reluctant to add more virtual filesystem methods to fiona's API. That's not a good direction for the project. Instead, I recommend that torchgeo use a dedicated package like fsspec or the Azure Python module.