Mount Google Drive in Google Colab for Persistent Storage
Learn how to mount Google Drive in a Colab notebook so that models, datasets, and configs persist after runtime disconnections.
09 Jul 2025, 19:29 UTC

Desired outcome
Make files saved in a Colab notebook survive runtime disconnections by linking the notebook’s filesystem to a folder in your Google Drive.
Prerequisites
- A Google account with access to Google Drive.
- Permission to run code in a Colab notebook (no special admin rights required).
- The notebook must be using a Python runtime (the default).
Procedure
Import the Drive integration module and start the mount process. Run this cell in the notebook:
from google.colab import drive drive.mount('/content/drive')When the cell executes, an authentication link appears. Click the link, sign in with your Google account, and copy the authorization code back into the prompted input box.
After successful authentication, the module creates a virtual directory at
/content/drive. Your Drive’s root folder is accessible via/content/drive/MyDrive.Verify the mount by listing the contents of your Drive:
!ls -l /content/drive/MyDriveYou should see the files and folders that exist in the root of your Google Drive.
Perform a quick write‑read test to confirm persistent I/O works:
test_path = '/content/drive/MyDrive/colab_test.txt' with open(test_path, 'w') as f: f.write('Persistent storage check') with open(test_path, 'r') as f: print(f.read())If the cell prints the string you wrote, the mount is functional.
Use standard Python file operations or shell commands (
!cp,!mkdir, etc.) on any path under/content/drive/MyDriveto store models, datasets, or configuration files that you want to keep after the session ends.
Expected checks
- The authentication prompt appears the first time you run
drive.mountin a session. !ls /content/drive/MyDrivereturns a listing matching your Drive’s web UI.- A test file created in the mounted directory appears in Google Drive within a few seconds (you can refresh the Drive UI to confirm).
Limitations and practical verification
- Drive I/O is slower than the VM’s local disk; avoid tight loops that read or write many small files.
- Directories with thousands of small files can cause mount timeouts; consider archiving them into a
.zipor.tarbefore transferring. - The mount is tied to the current runtime; if the runtime is recycled, you must re‑run the mount command in a new session.
Recovery options
If you need to detach the Drive (for example, to free resources or troubleshoot), run:
drive.flush_and_unmount()
This clears the mount point; you can remount later by repeating the authentication steps.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.