Skip to content

Commit 5be465b

Browse files
authored
Fixed PermissionError on windows (#6170)
Fixed PermissionError that occurred when downloading PDF files via http in BasePDFLoader on windows. When downloading PDF files via http in BasePDFLoader, NamedTemporaryFile is used. This function cannot open the file again on **Windows**.[Python Doc](https://docs.python.org/3.9/library/tempfile.html#tempfile.NamedTemporaryFile) So, we created a **temporary directory** with TemporaryDirectory and placed the downloaded file there. temporary directory is deleted in the deconstruct. Fixes #2698 #### Who can review? Tag maintainers/contributors who might be interested: - @eyurtsev - @hwchase17
1 parent 4fc7939 commit 5be465b

File tree

1 file changed

+7
-5
lines changed
  • langchain/document_loaders

1 file changed

+7
-5
lines changed

langchain/document_loaders/pdf.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ def __init__(self, file_path: str):
6262
)
6363

6464
self.web_path = self.file_path
65-
self.temp_file = tempfile.NamedTemporaryFile()
66-
self.temp_file.write(r.content)
67-
self.file_path = self.temp_file.name
65+
self.temp_dir = tempfile.TemporaryDirectory()
66+
temp_pdf = Path(self.temp_dir.name) / "tmp.pdf"
67+
with open(temp_pdf, mode="wb") as f:
68+
f.write(r.content)
69+
self.file_path = str(temp_pdf)
6870
elif not os.path.isfile(self.file_path):
6971
raise ValueError("File path %s is not a valid file or url" % self.file_path)
7072

7173
def __del__(self) -> None:
72-
if hasattr(self, "temp_file"):
73-
self.temp_file.close()
74+
if hasattr(self, "temp_dir"):
75+
self.temp_dir.cleanup()
7476

7577
@staticmethod
7678
def _is_valid_url(url: str) -> bool:

0 commit comments

Comments
 (0)