如何将本地数据集批量上传至Google Colab?
Got it, let's tackle this—uploading an entire local dataset to Colab without manually picking each file is totally doable. Here are a few reliable methods I've used myself for different scenarios:
方法1:上传压缩包然后解压(适合中小数据集)
This is the quickest fix if your dataset isn't massive. Instead of uploading dozens/hundreds of files, bundle them into one compressed file first:
- On your local machine, zip or tar.gz your entire dataset folder (right-click > "Compress" on most systems).
- In Colab, click the 📁 icon in the left sidebar, then hit the "Upload" button and select your compressed file.
- Once uploaded, run the corresponding command to extract it:
- For zip files:
!unzip your_dataset.zip - For tar.gz files:
!tar -xzf your_dataset.tar.gz
- For zip files:
- After running the command, you'll see your full dataset folder in the file browser—ready to use.
方法2:挂载Google Drive(适合大数据集/长期使用)
If your dataset is large (10GB+) or you need to reuse it across multiple Colab sessions, this is the way to go. It avoids repeated uploads and is more stable:
- First, sync your local dataset folder to your Google Drive: Use the Google Drive desktop app (for seamless background sync) or drag the folder directly into your Drive via the web interface.
- In Colab, run this code to mount your Drive:
from google.colab import drive drive.mount('/content/drive') - Follow the prompt: Click the generated link, log into your Google account, copy the authorization code, paste it back into Colab's input box, and press Enter.
- Once mounted, your dataset will be accessible at
/content/drive/MyDrive/[your_dataset_folder_path]—you can reference this path directly in your code.
方法3:直接上传整个文件夹(适合中小型 folders)
Colab has a built-in method to upload entire folders without compression, though it's best for smaller folders (since upload speed depends on your network):
- Run this code in a Colab cell:
from google.colab import files uploaded = files.upload_directory() - A file picker will pop up—select your local dataset folder, and Colab will upload all its contents directly to the
/contentdirectory.
Quick Tips
- Always verify the upload worked with
!ls(lists files in the current directory) or!ls /content/drive/MyDrive(for Drive-mounted files). - For extra large datasets, skip browser-based uploads entirely—use the Drive desktop app to sync locally first, then mount in Colab.
内容的提问来源于stack exchange,提问作者mohamed_abdullah




