pythonfsspec

Initialize fsspec DirFileSystem from a URL


I want to initalize a fsspec filesystem based on a URL - both the protocol and the root directory. E.g. I could create a filesystem from gcs://my-bucket/prefix that would use my-bucket on GCS, or file:///tmp/test that would use the /tmp/test directory in the local filesystem.

It can be done easily with following 2-liner:

from fsspec.core import url_to_fs
from fsspec.implementations.dirfs import DirFileSystem

URL = 'file:///tmp/test'

root_fs, root_path = url_to_fs(URL)
fs = DirFileSystem(root_path, root_fs)

fs.open('foo') # This opens /tmp/test/foo if URL was file:///tmp/test

but it feels like there should be an API for that in fsspec directly.

Is there?


Solution

  • A fix at https://github.com/fsspec/filesystem_spec/pull/1201 will enable this as requested. Your URL will be like "dir::protocol://path/to/mount".