Mounting Google Drive in Colab for Persistent Data Access
Learn how to mount Google Drive in a Colab notebook, access files persistently, and avoid re‑uploading data each session.
31 Jul 2025, 20:17 UTC

When you start a new Colab notebook, any files you upload or create disappear as soon as the runtime recycles. This forces you to re‑upload datasets, model checkpoints, or logs every session, which wastes time and bandwidth. The solution is to mount your Google Drive so that the notebook treats a folder in your Drive as a regular filesystem that survives runtime restarts.
How the drive mount works
The google.colab library provides a drive.mount() function that initiates an OAuth flow. After you follow the generated link and grant access, Colab creates a virtual filesystem mounted at /content/drive. Inside this mount, MyDrive maps to the root of your Google Drive, letting you read and write files just like on a local disk.
Where to run the command
Execute the mount command in a code cell of your notebook. You need only a standard Colab account; no special admin rights are required. The cell will display a URL that you must open in a browser, sign in with the Google account that owns the Drive, and copy back the authorization code.
# Run this in a Colab code cell
from google.colab import drive
drive.mount('/content/drive')
After pasting the code and pressing Enter, the cell will show a link. Click it, allow access, copy the token, paste it into the cell, and press Enter again. If the mount succeeds, you will see a message indicating that the Drive is mounted at /content/drive.
Worked example: persisting a dataset
Suppose you have a CSV file data.csv stored in the root of your Drive. You want to use it across multiple notebook runs without re‑uploading.
- Mount the Drive as shown above.
- Access the file via the mounted path:
import pandas as pd
# Path inside the mounted Drive
df = pd.read_csv('/content/drive/MyDrive/data.csv')
print(df.head())
# Create a test file (run once)
with open('/content/drive/MyDrive/colab_test.txt', 'w') as f:
f.write('persistent check')
After you stop the runtime and start a new notebook, repeat the mount steps and run:
# Verify the file still exists
import os
print(os.path.exists('/content/drive/MyDrive/colab_test.txt')) # Should print True
Trade‑offs and limitations
While mounting gives you persistent storage, it is not a drop‑in replacement for a local SSD. The I/O latency is higher because each read/write request travels over the network to Google’s servers. For tight training loops that read large batches repeatedly, it is faster to copy the needed data to the VM’s temporary storage (/content) at the start of the notebook and write back only the final results.
Google Drive also enforces your personal storage quota. If you exceed the limit, write operations will fail even though the mount appears successful. You can check your usage in the Drive web UI or by running:
!df -h /content/drive
The output shows the size, used space, and available space for the mounted filesystem.
Actionable closing
Mounting Google Drive is a straightforward way to make your Colab workflows resilient to runtime recycling. Follow the mount command, verify with a small test file, and consider copying active datasets to the local VM for performance‑critical steps. When you notice slow file access or quota warnings, move intermediate outputs back to /content and keep only final artifacts in Drive.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.