diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 91e7c932..e808f5e4 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -166,6 +166,11 @@ class CloudPath(metaclass=CloudPathMeta): cloud_prefix: str def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client"] = None): + + # handle if local file gets opened. must be set at the top of the method in case any code + # below raises an exception, this prevents __del__ from raising an AttributeError + self._handle = None + self.is_valid_cloudpath(cloud_path, raise_on_error=True) # versions of the raw string that provide useful methods @@ -190,9 +195,6 @@ def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client # track if local has been written to, if so it may need to be uploaded self._dirty = False - # handle if local file gets opened - self._handle = None - def __del__(self): # make sure that file handle to local path is closed if self._handle is not None: