Skip to content

Commit 08b018b

Browse files
authored
raise in download_to if not exists (#433)
1 parent 71331d3 commit 08b018b

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Drop support for Python 3.7; pin minimal `boto3` version to Python 3.8+ versions. (PR [#407](https://github.com/drivendataorg/cloudpathlib/pull/407))
88
- fix: use native `exists()` method in `GSClient`. (PR [#420](https://github.com/drivendataorg/cloudpathlib/pull/420))
99
- Enhancement: lazy instantiation of default client (PR [#432](https://github.com/drivendataorg/cloudpathlib/issues/432), Issue [#428](https://github.com/drivendataorg/cloudpathlib/issues/428))
10+
- Adds existence check before downloading in `download_to` (Issue [#430](https://github.com/drivendataorg/cloudpathlib/issues/430), PR [#432](https://github.com/drivendataorg/cloudpathlib/pull/432))
1011

1112
## v0.18.1 (2024-02-26)
1213

cloudpathlib/cloudpath.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _make_selector(pattern_parts, _flavour, case_sensitive=True):
6363
CloudPathFileExistsError,
6464
CloudPathIsADirectoryError,
6565
CloudPathNotADirectoryError,
66+
CloudPathNotExistsError,
6667
CloudPathNotImplementedError,
6768
DirectoryNotEmptyError,
6869
IncompleteImplementationError,
@@ -899,6 +900,10 @@ def stat(self, follow_symlinks: bool = True) -> os.stat_result:
899900
# =========== public cloud methods, not in pathlib ===============
900901
def download_to(self, destination: Union[str, os.PathLike]) -> Path:
901902
destination = Path(destination)
903+
904+
if not self.exists():
905+
raise CloudPathNotExistsError(f"Cannot download because path does not exist: {self}")
906+
902907
if self.is_file():
903908
if destination.is_dir():
904909
destination = destination / self.name

cloudpathlib/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class CloudPathFileExistsError(CloudPathException, FileExistsError):
2020
pass
2121

2222

23+
class CloudPathNotExistsError(CloudPathException):
24+
pass
25+
26+
2327
class CloudPathIsADirectoryError(CloudPathException, IsADirectoryError):
2428
pass
2529

tests/test_cloudpath_file_io.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from cloudpathlib import CloudPath
1111

1212
from cloudpathlib.exceptions import (
13+
CloudPathNotExistsError,
1314
CloudPathIsADirectoryError,
1415
CloudPathNotImplementedError,
1516
DirectoryNotEmptyError,
@@ -396,6 +397,9 @@ def test_file_read_writes(rig, tmp_path):
396397
)
397398
assert cloud_rel_paths == dled_rel_paths
398399

400+
with pytest.raises(CloudPathNotExistsError):
401+
(p / "not_exists_file").download_to(dl_file)
402+
399403

400404
def test_dispatch_to_local_cache(rig):
401405
p = rig.create_cloud_path("dir_0/file0_1.txt")

0 commit comments

Comments
 (0)