import kagglehub
import shutil
import os
# Download latest version
path = kagglehub.dataset_download("aaron7sun/stocknews")
# Define the target directory
target_dir = os.path.join(os.path.dirname(__file__), '想要存放文件夹的位置')
# Ensure the target directory exists
os.makedirs(target_dir, exist_ok=True)
# Move the downloaded files to the target directory
for file_name in os.listdir(path):
shutil.move(os.path.join(path, file_name), os.path.join(target_dir, file_name))
print("Files have been moved to:", target_dir)
其中path是在kaggle中的download中出现。
值得注意的是在你想要下载的位置不需要设置一个预留的文件夹用于存放要下载的文件。从kaggle上下载之后会自带一个打包好的文件夹。所以想要存放的文件夹的位置为想要存放的位置的母目录。