diff --git a/python/cog/predictor.py b/python/cog/predictor.py index 6294212c5e..73786f0f4c 100644 --- a/python/cog/predictor.py +++ b/python/cog/predictor.py @@ -196,7 +196,10 @@ def cleanup(self) -> None: # Note this is pathlib.Path, which cog.Path is a subclass of. A pathlib.Path object shouldn't make its way here, # but both have an unlink() method, so may as well be safe. elif isinstance(value, Path): - value.unlink(missing_ok=True) + try: + value.unlink() + except FileNotFoundError: + pass def get_predict(predictor: Any) -> Callable: diff --git a/python/cog/types.py b/python/cog/types.py index 52842c1e3a..91d80b1b1c 100644 --- a/python/cog/types.py +++ b/python/cog/types.py @@ -123,8 +123,13 @@ def convert(self) -> Path: return self._path def unlink(self, missing_ok: bool = False) -> None: - if self._path and self._path.exists(): - self._path.unlink(missing_ok=missing_ok) + if self._path: + # TODO: use unlink(missing_ok=...) when we drop Python 3.7 support. + try: + self._path.unlink() + except FileNotFoundError: + if not missing_ok: + raise def __str__(self) -> str: # FastAPI's jsonable_encoder will encode subclasses of pathlib.Path by