Ich versuche, einen Kaggle-Datensatz herunterzuladen. Hier ist die Dokumentation zu https://www.kaggle.com/docs/api und https://github.com/Kaggle/kaggle-api. Eine Json-Datei muss heruntergeladen und in einem bestimmten Ordner abgelegt werden. Es ist wie folgt geschrieben:
This will trigger the download of kaggle.json, a file containing your API credentials. Place this file in the location ~/.kaggle/kaggle.json (on Windows in the location C:\Users<Windows-username>.kaggle\kaggle.json
Ich zeige meine Implementierung weiter unten. Der Code funktionierte zuvor in Colab / Jupyter Notebooks für Windows und Mac. Ich möchte weiterhin in PyCharm arbeiten und verwende derzeit Windows. Leider wird eine Fehlermeldung angezeigt:
Code: Alles auswählen
with open('/root/.kaggle/kaggle.json', 'w') as file:
FileNotFoundError: [Errno 2] No such file or directory: '/root/.kaggle/kaggle.json'
Wie kann ich den Ordner mit Hilfe von Pathlib universell angeben? Damit es später unter Windows, Mac und Linux funktioniert?
Code: Alles auswählen
username_kaggle = "dummy"
token_kaggle = "dummy"
savepath = "/retailrocket"
if not os.path.exists(savepath):
os.makedirs(savepath)
os.chdir(savepath)
# Download the dataset from kaggle
os.system("mkdir ~ /.kaggle")
os.system("touch ~ /.kaggle / kaggle.json")
api_token = {"username": username_kaggle, "key": token_kaggle}
with open('/root/.kaggle/kaggle.json', 'w') as file:
json.dump(api_token, file)
os.system("kaggle datasets download - d retailrocket / ecommerce - dataset")