初始化fsspec DirFileSystem从URL

huangapple go评论58阅读模式
英文:

Initialize fsspec DirFileSystem from a URL

问题

我想要根据URL初始化一个fsspec文件系统,包括协议和根目录。例如,我可以从gcs://my-bucket/prefix创建一个使用GCS上的my-bucket的文件系统,或者从file:///tmp/test创建一个使用本地文件系统中的/tmp/test目录的文件系统。

可以使用以下两行代码轻松完成:

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') # 如果URL是file:///tmp/test,这将打开/tmp/test/foo

但感觉在fsspec中应该有一个直接的API来实现这一点。

有吗?

英文:

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?

答案1

得分: 2

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

英文:

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

huangapple
  • 本文由 发表于 2023年3月4日 01:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定