Persisting Data in Google Colab: Mounting Google Drive for Reliable Storage
Mount Google Drive in Colab to overcome the runtime’s temporary storage. Learn how to authenticate, write checkpoints, and avoid common pitfalls like latency and API limits.
04 Aug 2025, 16:09 UTC

Why Mount Google Drive?
Google Colab runtimes are temporary virtual machines. When a session ends, any files written to /content disappear. For long‑running experiments, model checkpoints, and shared datasets you need a persistent store. Google Drive offers a convenient, cloud‑based file system that can be accessed directly from a Colab notebook via the google.colab library.
How to Mount and Use Drive in Colab
- Import the library:
from google.colab import drive - Mount the drive to the standard path
/content/drive.drive.mount('/content/drive')When this command runs, Colab opens an OAuth2 consent screen. Grant access, copy the provided code, and paste it back into the notebook cell. After successful authentication, a symbolic link is created that points
/content/driveto the root of your Google Drive. - Verify the mount:
!ls /content/drive/MyDriveThe output should list the contents of your Drive’s
MyDrivefolder.
Practical Example: Saving a Model Checkpoint
Below is a minimal PyTorch training loop that writes a checkpoint to the mounted drive. The checkpoint survives a runtime restart, demonstrating persistence.
import torch
import torch.nn as nn
import torch.optim as optim
# Simple network
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(10, 1)
def forward(self, x):
return self.fc(x)
model = Net()
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.MSELoss()
# Dummy data
x = torch.randn(4, 10)
y = torch.randn(4, 1)
for epoch in range(5):
optimizer.zero_grad()
out = model(x)
loss = criterion(out, y)
loss.backward()
optimizer.step()
# Save checkpoint
checkpoint_path = '/content/drive/MyDrive/checkpoints/epoch_{}.pt'.format(epoch)
torch.save(model.state_dict(), checkpoint_path)
print(f"Saved epoch {epoch} to {checkpoint_path}")
After the loop, you can inspect MyDrive/checkpoints in the Drive web UI or via another notebook cell to confirm the files exist. Restart the runtime, remount the drive, and reload the state dict to resume training.
Limitations and Common Pitfalls
- Network Latency: Drive I/O is performed over the network. Frequent small‑file reads/writes can be noticeably slower than local
/contentstorage. Batch your I/O or move large data files to/contentbefore training. - API Rate Limits: Google Drive imposes per‑second request limits. When looping over thousands of files (e.g., for dataset shuffling) you may hit
429 Too Many Requests. Usetime.sleep()or cache files locally. - Authentication Expiry: The OAuth2 token is tied to the Colab session. If the runtime restarts, you must re‑run
drive.mount()and re‑enter the code. Automate this step in notebooks that are often re‑loaded. - Permission Issues: Ensure the Drive folder you target is owned by the same Google account that authorized the notebook. Shared folders require explicit permission settings.
- File System Quotas: Google Drive has a 15 GB free quota (plus any Google Workspace allotment). Exceeding this can cause writes to fail.
Best Practices for Large Datasets
- Store raw data in Drive, but copy it to
/contentonce per session usingshutil.copytree()orrsyncto benefit from faster local I/O during training. - Compress large files (e.g.,
.zipor.tar.gz) before uploading to reduce API calls. - Use
google.colab.files.upload()for small auxiliary files that don’t merit Drive storage. - Clean up
/content/driveafter training to avoid cluttering your Drive.
Verification Checklist
- Run
drive.mount('/content/drive')and confirm the OAuth flow completes. - Execute
!ls /content/drive/MyDriveto see your Drive contents. - Write a test file:
!echo 'test' > /content/drive/MyDrive/test.txtand verify it appears in the Drive UI. - Restart the runtime, remount, and confirm
test.txtstill exists.
Following these steps guarantees that your experiment data remains intact across Colab sessions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.