From 2c57a79f5f9788c807c39583932ad35115bbae69 Mon Sep 17 00:00:00 2001 From: Panagiotis Simakis Date: Sun, 12 Dec 2021 22:23:16 +0200 Subject: [PATCH 1/2] replace `os.mkdir` with `os.makedirs` to prevent `FileNotFoundError` when trying to use non-existing nested folders --- AUTHORS.md | 1 + nltk/downloader.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 04592e11cc..8c73455774 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -286,6 +286,7 @@ - Ahmet Yildirim - Yuta Nakamura - Adam Hawley +- Panagiotis Simakis ## Others whose work we've taken and included in NLTK, but who didn't directly contribute it: diff --git a/nltk/downloader.py b/nltk/downloader.py index e513435a5a..78b0d0ee01 100644 --- a/nltk/downloader.py +++ b/nltk/downloader.py @@ -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. From 8305fe8861e673a67be0c1e2d06b57c119d15cc0 Mon Sep 17 00:00:00 2001 From: Panagiotis Simakis Date: Mon, 13 Dec 2021 10:57:29 +0200 Subject: [PATCH 2/2] add unittests for download_dir --- nltk/test/unit/test_downloader.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 nltk/test/unit/test_downloader.py diff --git a/nltk/test/unit/test_downloader.py b/nltk/test/unit/test_downloader.py new file mode 100644 index 0000000000..036b56bb4f --- /dev/null +++ b/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