要将文件从 Kaggle 上传到 Google Drive,您可以按照以下步骤操作:
shutil
库将文件从 Colab 的文件系统上传到 Google Drive:import shutil import os # 下载文件的路径 downloaded_file_path = '/content/competition-name/file.zip' # Google Drive 挂载路径 drive_path = '/content/drive/MyDrive/' # 上传文件到 Google Drive shutil.copy(downloaded_file_path, drive_path)以下是一个示例脚本:
import os
import kaggle
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
# Kaggle API 配置
kaggle.api.authenticate()
# Google Drive API 配置
credentials = service_account.Credentials.from_service_account_file(
'path/to/your/service-account-file.json',
scopes=['https://www.googleapis.com/auth/drive']
)
drive_service = build('drive', 'v3', credentials=credentials)
# 下载 Kaggle 文件
kaggle.api.competition_download_files('competition-name')
# 上传文件到 Google Drive
file_path = 'competition-name/file.zip'
file_metadata = {'name': 'file.zip', 'parents': ['YOUR_DRIVE_FOLDER_ID']}
media = MediaFileUpload(file_path, mimetype='application/zip')
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print(f'File ID: {file.get("id")}')
通过以上步骤,您可以将文件从 Kaggle 上传到 Google Drive。根据您的具体需求和环境,选择合适的方法进行操作。
领取专属 10元无门槛券
手把手带您无忧上云