0

I have written a small program code which automatically downloads something from Kaggle. The whole thing should be downloaded to the folder C:/Users/User/Documents/dataset. Here is the problem, when I run the code the first time the path is correct. If I run the code again I get the following path C:/Users/User/Documents/dataset/dataset. The problem is the folder dataset is added every time the program is executed. What is the reason for this?

I use Jupyter Notebook under Anaconda

import pathlib
import json
import os

dir_path = ""
dataset_name = "stroke-prediction-dataset"
dir_path = pathlib.Path("").resolve()
save_path = dir_path / "dataset"

# Therefore it also never recognizes this If statement
# because it always creates a new folder
if (save_path  / pathlib.Path(dataset_name+".zip")).is_file() == False:

    USERNAME_KAGGLE = "..."
    TOKEN_KAGGLE = "..."



    if not os.path.exists(save_path):
        os.makedirs(save_path)
    os.chdir(save_path)

    # Download the dataset from kaggle
    !pip install kaggle

    print()
    api_token = {"username":USERNAME_KAGGLE,"key":TOKEN_KAGGLE}
    with open(os.path.expanduser('~') + '/.kaggle/kaggle.json', 'w') as file:
        json.dump(api_token, file)
    !kaggle datasets download -d fedesoriano/stroke-prediction-dataset
Kazim
  • 175
  • 9
  • The problem is, I don't run it directly in the folder. I get this Path `C:/Users/User/Documents`. But I want the dataset to be saved in the `/dataset` folder, so I create a new one. If it does not exist yet, I create this one. – Kazim May 14 '21 at 16:46
  • 2
    The problem is, you're doing `os.chdir(save_path)` and so the subsequent times you run this code you *are* in the subdirectory. You can avoid this by not issuing that command, and then saving into the `save_path` directory. – mechanical_meat May 14 '21 at 17:02
  • @mechanical_meat As soon as I remove `os.chdir(save_path)` it only saves to `C:/Users/User/Documents` and no longer to the subfolder – Kazim May 14 '21 at 17:33
  • what does `!kaggle datasets download -d fedesoriano/stroke-prediction-dataset` do ? – Umar.H May 18 '21 at 10:49
  • This performs an automated download of the Kaggle dataset `fedesoriano/stroke-prediction-dataset` – Kazim May 18 '21 at 11:01

0 Answers0