Skip to content

The agent engineering platform.

Fixed PermissionError on windows (#6170)

MIDORIBIN5be465bd86f9
diff --git a/langchain/document_loaders/pdf.py b/langchain/document_loaders/pdf.py
index 5f204f1f7f..1d334e097a 100644
--- a/langchain/document_loaders/pdf.py
+++ b/langchain/document_loaders/pdf.py
@@ -62,15 +62,17 @@ class BasePDFLoader(BaseLoader, ABC):
)
self.web_path = self.file_path
- self.temp_file = tempfile.NamedTemporaryFile()
- self.temp_file.write(r.content)
- self.file_path = self.temp_file.name
+ self.temp_dir = tempfile.TemporaryDirectory()
+ temp_pdf = Path(self.temp_dir.name) / "tmp.pdf"
+ with open(temp_pdf, mode="wb") as f:
+ f.write(r.content)
+ self.file_path = str(temp_pdf)
elif not os.path.isfile(self.file_path):
raise ValueError("File path %s is not a valid file or url" % self.file_path)
def __del__(self) -> None:
- if hasattr(self, "temp_file"):
- self.temp_file.close()
+ if hasattr(self, "temp_dir"):
+ self.temp_dir.cleanup()
@staticmethod
def _is_valid_url(url: str) -> bool: