Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FileNotFoundError when the download_dir is a non-existing nested folder #2910

Merged
merged 2 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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