2025, Nov 27 01:00

Upload Files and Folders to Google Drive from Colab: Mount Drive, Use Stable Paths, Add a Folder Picker

Learn to mount Google Drive in Colab and upload files directly to MyDrive for persistent, stable paths. Includes an ipywidgets folder picker for non-coders.

When a Colab notebook is the primary interface for non-coders, you want file input to be frictionless. A common stumbling block is that files uploaded with the default helper land in the notebook’s ephemeral runtime, not in Google Drive. If you need users to upload a video folder once and have the notebook refer to it consistently during execution, you should direct uploads straight into Drive.

Problem setup

Using the default upload helper puts files into temporary storage that disappears with the runtime. That’s not ideal if your workflow expects stable paths in Drive, especially when users upload large folders with dozens of .avi files (1–2 GB).

from google.colab import files as colab_files

colab_files.upload()

What’s going on

The helper above targets the notebook’s local filesystem by default. Google Drive is not the same location; it must be mounted into the runtime first, and then explicitly used as the destination for uploads. Without that, files won’t appear in Drive and your code can’t access them via Drive paths.

Solution: mount Drive and upload to a target folder

First, mount Google Drive in the runtime so it becomes available under a known path. Then pass that Drive path to the upload function. From there, your notebook can reliably reference the uploaded data by absolute path.

from google.colab import drive as gdrive

gdrive.mount('/content/drive')

After mounting, upload directly into MyDrive (or any subfolder you control):

from google.colab import files as colab_files

colab_files.upload('/content/drive/MyDrive')

You can also work with the files via standard Python modules and libraries once they’re in Drive:

import os

os.listdir('/content/drive/MyDrive')
import pandas as pds

frame = pds.read_csv('/content/drive/MyDrive/my_data.csv')

Reference: External data: Local Files, Drive, Sheets, and Cloud Storage — Colab: https://colab.research.google.com/notebooks/io.ipynb?hl=en#scrollTo=7Z2jcRKwUHqV

Optional: let users pick a Drive folder via a dropdown

For a smoother UX, you can present a Drive directory list and upload into the folder the user selects. This avoids hardcoding paths and keeps the flow accessible.

import os
import ipywidgets as ui
from IPython.display import display
from google.colab import files as colab_files

root_dir = '/content/drive/MyDrive'
entries = os.listdir(root_dir)
dirnames = [nm for nm in entries if os.path.isdir(os.path.join(root_dir, nm))]

# Make the base Drive folder selectable as the first option
dirnames.insert(0, '.')

dir_picker = ui.Dropdown(
    options=dirnames,
    description='Select folder',
)

def handle_select(evt):
    chosen = evt['new']
    target_dir = os.path.join(root_dir, chosen)
    print('name:', chosen)
    print('full:', target_dir)
    colab_files.upload(target_dir)

dir_picker.observe(handle_select, names='value')
display(dir_picker)

More on widgets: Dropdown, Select, and File Upload in the ipywidgets docs: dropdown, select, file-upload.

Why this matters

When your audience isn’t writing code, predictable paths and repeatable behavior are everything. Mounting Drive and uploading to a deterministic location eliminates manual path entry and makes the rest of the notebook straightforward to automate and maintain.

Takeaways

Mount Drive at the start of the session, target a Drive path when uploading, and use absolute paths for downstream processing. If your users benefit from guided selection, add a simple widget-driven folder picker to keep the workflow clear and consistent.