diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index d2e1fcc3..6d285e36 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -207,6 +207,11 @@ def __hash__(self): def __eq__(self, other: Any): return repr(self) == repr(other) + def __fspath__(self): + if self.is_file(): + self._refresh_cache(force_overwrite_from_cloud=False) + return str(self._local) + # ====================== NOT IMPLEMENTED ====================== # absolute - no cloud equivalent; all cloud paths are absolute already # as_posix - no cloud equivalent; not needed since we assume url separator @@ -277,6 +282,10 @@ def as_uri(self) -> str: def exists(self) -> bool: return self.client._exists(self) + @property + def fspath(self) -> str: + return self.__fspath__() + def glob(self, pattern: str) -> Iterable["CloudPath"]: # strip cloud prefix from pattern if it is included if pattern.startswith(self.cloud_prefix): diff --git a/tests/test_cloudpath_instantiation.py b/tests/test_cloudpath_instantiation.py index 6495de55..e70757f0 100644 --- a/tests/test_cloudpath_instantiation.py +++ b/tests/test_cloudpath_instantiation.py @@ -1,3 +1,5 @@ +import os + import pytest from cloudpathlib import AzureBlobPath, CloudPath, InvalidPrefix, MissingDependencies, S3Path @@ -60,3 +62,19 @@ def test_dependencies_not_loaded(rig, monkeypatch): CloudPath(f"{rig.cloud_prefix}/bucket/dir_0/file0_0.txt") with pytest.raises(MissingDependencies): rig.create_cloud_path("bucket/dir_0/file0_0.txt") + + +def test_is_pathlike(rig): + p = rig.create_cloud_path("bucket") + assert isinstance(p, os.PathLike) + + +def test_fspath(rig): + p = rig.create_cloud_path("bucket") + os.fspath(p) + + +def test_os_open(rig): + p = rig.create_cloud_path("bucket/dir_0/file0_0.txt") + with open(p, "r") as f: + assert f.readable()