diff --git a/bindings/python/python/opendal/__init__.pyi b/bindings/python/python/opendal/__init__.pyi index 6d6645a60bbc..e95cdfff7d31 100644 --- a/bindings/python/python/opendal/__init__.pyi +++ b/bindings/python/python/opendal/__init__.pyi @@ -48,6 +48,7 @@ class Operator: def copy(self, source: str, target: str) -> None: ... def rename(self, source: str, target: str) -> None: ... def remove_all(self, path: str) -> None: ... + def exists(self, path: str) -> bool: ... def to_async_operator(self) -> AsyncOperator: ... @final @@ -81,6 +82,7 @@ class AsyncOperator: async def copy(self, source: str, target: str) -> None: ... async def rename(self, source: str, target: str) -> None: ... async def remove_all(self, path: str) -> None: ... + async def exists(self, path: str) -> bool: ... def to_operator(self) -> Operator: ... @final diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs index 76cdb09f8653..b650d1ce759f 100644 --- a/bindings/python/src/operator.rs +++ b/bindings/python/src/operator.rs @@ -398,6 +398,13 @@ impl AsyncOperator { }) } + pub fn exists<'p>(&'p self, py: Python<'p>, path: String) -> PyResult> { + let this = self.core.clone(); + future_into_py( + py, + async move { this.exists(&path).await.map_err(format_pyerr) }, + ) + } /// Create a dir at given path. /// /// # Notes diff --git a/bindings/python/tests/test_exists.py b/bindings/python/tests/test_exists.py new file mode 100644 index 000000000000..cba26c70cb9d --- /dev/null +++ b/bindings/python/tests/test_exists.py @@ -0,0 +1,29 @@ +import os +from random import randint +from uuid import uuid4 + +import pytest + + +@pytest.mark.need_capability("write", "delete", "stat") +def test_sync_exists(service_name, operator, async_operator): + size = randint(1, 1024) + filename = f"test_file_{str(uuid4())}.txt" + content = os.urandom(size) + size = len(content) + operator.write(filename, content) + assert operator.exists(filename) + assert not operator.exists(filename + "not_exists") + + +@pytest.mark.asyncio +@pytest.mark.need_capability("write", "delete", "stat") +async def test_async_exists(service_name, operator, async_operator): + size = randint(1, 1024) + filename = f"test_file_{str(uuid4())}.txt" + content = os.urandom(size) + size = len(content) + await async_operator.write(filename, content) + assert await async_operator.exists(filename) + assert not await async_operator.exists(filename + "not_exists") + await async_operator.delete(filename)