Pyzotero: a Python client for the Zotero API pyzotero.readthedocs.io
zotero

Simplify file verification in _upload.py

+7 -12
+7 -12
src/pyzotero/_upload.py
··· 56 56 if not payload: # Check payload has nonzero length 57 57 raise ze.ParamNotPassedError 58 58 for templt in payload: 59 - if Path(str(self.basedir.joinpath(templt["filename"]))).is_file(): 60 - try: 61 - # if it is a file, try to open it, and catch the error 62 - with Path(str(self.basedir.joinpath(templt["filename"]))).open(): 63 - pass 64 - except OSError: 65 - msg = f"The file at {self.basedir.joinpath(templt['filename'])!s} couldn't be opened or found." 66 - raise ze.FileDoesNotExistError(msg) from None 67 - # no point in continuing if the file isn't a file 68 - else: 69 - msg = f"The file at {self.basedir.joinpath(templt['filename'])!s} couldn't be opened or found." 70 - raise ze.FileDoesNotExistError(msg) 59 + filepath = self.basedir.joinpath(templt["filename"]) 60 + try: 61 + with filepath.open(): 62 + pass 63 + except OSError: 64 + msg = f"The file at {filepath!s} couldn't be opened or found." 65 + raise ze.FileDoesNotExistError(msg) from None 71 66 72 67 def _create_prelim(self) -> dict | None: 73 68 """Step 0: Register intent to upload files."""