Skip to content

Commit

Permalink
Fix FileNotFoundError when the download_dir is a non-existing nes…
Browse files Browse the repository at this point in the history
…ted folder (#2910)

* replace `os.mkdir` with `os.makedirs` to prevent `FileNotFoundError` when trying to use non-existing nested folders

* add unittests for download_dir
  • Loading branch information
sp1thas committed Dec 13, 2021
1 parent abbe86b commit dea7b44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -286,6 +286,7 @@
- Ahmet Yildirim <https://github.com/RnDevelover>
- Yuta Nakamura <https://github.com/yutanakamura-tky>
- Adam Hawley <https://github.com/adamjhawley>
- Panagiotis Simakis <https://github.com/sp1thas>

## Others whose work we've taken and included in NLTK, but who didn't directly contribute it:

Expand Down
4 changes: 2 additions & 2 deletions nltk/downloader.py
Expand Up @@ -695,9 +695,9 @@ def _download_package(self, info, download_dir, force):

# Ensure the download_dir exists
if not os.path.exists(download_dir):
os.mkdir(download_dir)
os.makedirs(download_dir)
if not os.path.exists(os.path.join(download_dir, info.subdir)):
os.mkdir(os.path.join(download_dir, info.subdir))
os.makedirs(os.path.join(download_dir, info.subdir))

# Download the file. This will raise an IOError if the url
# is not found.
Expand Down
19 changes: 19 additions & 0 deletions nltk/test/unit/test_downloader.py
@@ -0,0 +1,19 @@
from nltk import download


def test_downloader_using_existing_parent_download_dir(tmp_path):
"""Test that download works properly when the parent folder of the download_dir exists"""

download_dir = str(tmp_path.joinpath("another_dir"))
download_status = download("mwa_ppdb", download_dir)
assert download_status is True


def test_downloader_using_non_existing_parent_download_dir(tmp_path):
"""Test that download works properly when the parent folder of the download_dir does not exist"""

download_dir = str(
tmp_path.joinpath("non-existing-parent-folder", "another-non-existing-folder")
)
download_status = download("mwa_ppdb", download_dir)
assert download_status is True

0 comments on commit dea7b44

Please sign in to comment.