Persisting Data in Google Colab via Google Drive Mounting
Learn how to mount Google Drive in Google Colab to persist datasets and model weights across sessions, including implementation code and performance tips.
20 Dec 2025, 22:35 UTC

The Problem: Ephemeral Runtime Storage
Google Colab provides a temporary virtual machine for every session. Any data uploaded directly to the session storage (the /content folder) is deleted as soon as the runtime is recycled or disconnected. To avoid re-uploading large datasets or losing trained model weights, you must link a persistent storage layer. The most direct method is mounting Google Drive, which maps your cloud storage as a local directory within the Colab environment.
How to Mount Google Drive
Mounting is achieved using the google.colab Python library. This process creates a bridge between the Colab VM and your Google Drive account, making your files accessible via standard Linux file paths.
Implementation Example
Run the following code in a Colab cell. When executed, Colab will prompt you to authorize access to your Google account via a pop-up window or a verification link.
from google.colab import drive
# This command initiates the mounting process
drive.mount('/content/drive')
Verifying the Connection
Once the mount is successful, your Drive files are located under /content/drive/MyDrive. You can verify the connection by listing the files in your root Drive folder using a shell command. Run this in a new cell:
# Run this in a Colab cell to list files in your Drive
!ls -l /content/drive/MyDrive
Working with Files
Because the drive is mounted as a filesystem, you can use standard Python open() functions or shell commands to manage data. For example, to save a CSV file directly to a specific folder in your Drive:
import pandas as pd
# Sample data
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
# Define the path (ensure the folder 'colab_data' exists in your Drive)
path = '/content/drive/MyDrive/colab_data/results.csv'
df.to_csv(path, index=False)
print(f"File saved to {path}")
Operational Limitations
- Session Persistence: The mount is not permanent. If the runtime disconnects due to inactivity or the maximum session limit is reached, the virtual machine is destroyed. You must re-run the
drive.mount()cell every time you start a new session. - Storage Quotas: Colab does not provide additional storage for this feature. The available space is strictly limited by your Google Drive account quota (e.g., 15GB for free accounts).
- I/O Latency: Because the files are being accessed over a network bridge, reading thousands of small files (like an image dataset for deep learning) can be significantly slower than reading from the local
/contentdisk.
Common Pitfalls and Security
The "Missing Folder" Error
A common mistake is attempting to write to a subdirectory that does not yet exist in Google Drive. Python's to_csv or open() will not automatically create missing folders. You must create the directory via the Drive web interface or use os.makedirs('/content/drive/MyDrive/your_folder', exist_ok=True) before saving files.
Security Risks
Mounting your drive grants the notebook full read/write access to your entire Google Drive. Avoid sharing notebooks containing mount code with untrusted users if the notebook also contains scripts that could delete or modify your files. Never store plain-text API keys or credentials in files on your mounted drive if the notebook is public.
Performance Optimization
If you are training a model on a large dataset stored in Drive, the network overhead can bottleneck your GPU. The recommended pattern is to store your dataset as a single compressed .zip or .tar.gz file on Drive, copy it to the local runtime, and unzip it there:
# Copy from Drive to local ephemeral storage for faster access
!cp /content/drive/MyDrive/dataset.zip /content/dataset.zip
!unzip -q /content/dataset.zip -d /content/dataset
Rollback and Disconnection
To manually unmount the drive and revoke the session's access to your files without restarting the runtime, use the following command:
drive.flush_and_unmount()
This ensures all pending writes are completed before the connection is severed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.