https://opendal.apache.org/ logo
Can someone explain what the root
# general
j
Can someone explain what the root argument is used for? Is this where temporary operations are performed in the event of reads/writes? It seems odd to have to do the following... (I am attempting to implement a Pathlib like interface over OpenDAL I intend to propose to pyiceberg in favor of fsspec and pyarrow file systems)```py from urllib.parse import urlparse, ParseResult from opendal import Operator from pathlib import Path class OperatorHelper: def __init__(self, operator: Operator, parsed: ParseResult, path: Path) -> None: self.operator = operator self.parsed = parsed self.path = path class FileSystem: @staticmethod def _get_operator_helper(path: str, **options) -> OperatorHelper: """Get the OperatorHelper for the given path. Args: path: The path to the file or directory. Returns: OperatorHelper """ parsed = urlparse(path) scheme = parsed.scheme netloc = parsed.netloc path = Path(parsed.path) root = str(path.parent) if scheme in ("fs", ""): operator = Operator("fs", root=root, **options) elif scheme in ("s3", "gcs"): operator = Operator(scheme, root=root, bucket=netloc) return OperatorHelper(operator, parsed, path) def read(self, path: str, **options): helper = self._get_operator_helper(path, **options) name = helper.path.name return helper.operator.read(name) file_system = FileSystem() ```
x
Hi,
root
is the base dir for all your following operations. That's said that if you set
root
to
/abc
, then you can just use
op.read("def.pdf")
instead of specifying
/abc/def.pdf
everytime.
So if you always has the correct path of your file, you can just use
/
as root.
j
I see, so id need to do something like
Copy code
py
from opendal import Operator
from pathlib import Path
import os


os.makedirs("foo", exist_ok=True)

file = Path("foo", "bar.txt")
file.touch()
file.write_text("baz")


operator = Operator("fs", root="/")

with operator.open(str(file.resolve()), "rb") as f:
    print(f.read())
And that will actually work