DEV Community

Cover image for File Transfer Between Google Colab and VS Code: Uploading & Downloading Files Directly
CHITRAVANSH MOHAN
CHITRAVANSH MOHAN

Posted on

File Transfer Between Google Colab and VS Code: Uploading & Downloading Files Directly

While working on my 100 Days of Machine Learning journey, I started using the Google Colab VS Code Extension.

This setup is quite convenient because I can use the familiar VS Code interface while my notebook is actually running on a Google Colab cloud runtime.

However, I ran into a simple but important problem:

How do I transfer files between my local Windows machine and the remote Colab runtime when I'm working through VS Code?

For example:

Local Windows PC
       ↓
    VS Code
       ↓
Google Colab VS Code Extension
       ↓
Google Colab Cloud Runtime
       ↓
     /content/
Enter fullscreen mode Exit fullscreen mode

The problem becomes particularly noticeable when working with datasets, generated HTML reports, CSV files, images, etc.


🔴 The Problem

In normal browser-based Google Colab, we can use:

from google.colab import files

files.download("titanic_data_profiling.html")
Enter fullscreen mode Exit fullscreen mode

But when using Google Colab through the VS Code extension, this approach doesn't work reliably because the normal Colab browser interface isn't handling the download.

I also initially considered running an HTTP server inside Colab.

For example:

from http.server import HTTPServer, SimpleHTTPRequestHandler

server = HTTPServer(("0.0.0.0", 8000), SimpleHTTPRequestHandler)
Enter fullscreen mode Exit fullscreen mode

But this introduced another important concept.

localhost is not necessarily your local machine

The Python code is running on the Colab cloud VM, while VS Code is running on my local Windows machine.

Therefore:

localhost:8000
Enter fullscreen mode Exit fullscreen mode

inside the Colab runtime refers to the Colab environment, not my Windows computer.

So I needed a solution that actually understands the separation between:

LOCAL MACHINE
      ↕
VS CODE
      ↕
COLAB CLOUD RUNTIME
Enter fullscreen mode Exit fullscreen mode

🟢 Solution 1: Upload Files From Local PC → Colab

For uploading datasets from my Windows machine to the Colab runtime, I used the ipywidgets.FileUpload widget.

Step 1 — Create the upload widget

import ipywidgets as widgets
from IPython.display import display

uploader = widgets.FileUpload(
    accept='*',
    multiple=True
)

display(uploader)
Enter fullscreen mode Exit fullscreen mode

This gives me an upload button directly inside the notebook.

I can then select files from my local machine.

For example:

D:\datasets\titanic.csv
D:\datasets\train.csv
Enter fullscreen mode Exit fullscreen mode

Step 2 — Save the uploaded files

One important detail I discovered is that, in my VS Code + Colab environment, uploader.value behaves like a dictionary.

So instead of:

uploader.value[0]
Enter fullscreen mode Exit fullscreen mode

I use:

for filename, fileinfo in uploader.value.items():

    with open('/content/' + filename, 'wb') as f:
        f.write(fileinfo['content'])

    print(f'Uploaded: /content/{filename}')
Enter fullscreen mode Exit fullscreen mode

The result is something like:

Uploaded: /content/titanic.csv
Uploaded: /content/train.csv
Enter fullscreen mode Exit fullscreen mode

The files are now available inside the Colab runtime:

/content/
    ├── titanic.csv
    └── train.csv
Enter fullscreen mode Exit fullscreen mode

Step 3 — Verify the files

We can check the /content directory:

import os

print(os.listdir('/content'))
Enter fullscreen mode Exit fullscreen mode

The uploaded files should appear in the output.


Step 4 — Use the dataset normally

For example, with pandas:

import pandas as pd

df = pd.read_csv('/content/titanic.csv')

df.head()
Enter fullscreen mode Exit fullscreen mode

That's it.

The file has gone from:

Windows PC
     ↓
FileUpload widget
     ↓
Colab runtime
     ↓
/content/titanic.csv
Enter fullscreen mode Exit fullscreen mode

📥 Solution 2: Download Files From Colab → Local PC

Now let's consider the reverse situation.

Suppose I generate a YData Profiling report:

from ydata_profiling import ProfileReport

profile = ProfileReport(titanic)

profile.to_file('titanic_data_profiling.html')
Enter fullscreen mode Exit fullscreen mode

The file is created inside the Colab runtime:

/content/titanic_data_profiling.html
Enter fullscreen mode Exit fullscreen mode

I wanted to bring this file back to my Windows machine.


Using the Colab Contents View

The Google Colab VS Code Extension provides a Contents View for accessing the remote Colab filesystem.

In VS Code:

Ctrl + Shift + P
Enter fullscreen mode Exit fullscreen mode

Then search for:

Colab: Focus on Contents View
Enter fullscreen mode Exit fullscreen mode

This opens the Colab filesystem.

For example:

COLAB
└── CONTENTS
    └── Colab CPU
        ├── .config
        ├── sample_data
        └── titanic_data_profiling.html
Enter fullscreen mode Exit fullscreen mode

I can locate:

/content/titanic_data_profiling.html
Enter fullscreen mode Exit fullscreen mode

and use the available download/save functionality to transfer it back to my local machine.

This worked for me with the generated HTML profiling report.


🧠 Understanding the Architecture

This experience helped me understand something important about cloud development environments.

When working with the Colab VS Code Extension, there are effectively two environments involved.

Local environment

Windows
   │
   └── VS Code
Enter fullscreen mode Exit fullscreen mode

Remote environment

Google Colab
   │
   └── /content/
Enter fullscreen mode Exit fullscreen mode

The VS Code interface doesn't mean that the Python process is running on your local machine.

The notebook kernel is running on the Colab cloud runtime.

Therefore, a file created with:

open('/content/example.csv', 'w')
Enter fullscreen mode Exit fullscreen mode

exists on the Colab VM.

It does not automatically appear in:

D:\programs\Machine_Learning\...
Enter fullscreen mode Exit fullscreen mode

on my Windows machine.

Understanding this distinction made the whole problem much clearer.


⚠️ What I Initially Tried

1. files.download()

In browser-based Colab:

from google.colab import files

files.download('example.html')
Enter fullscreen mode Exit fullscreen mode

is a convenient solution.

However, when using Colab through the VS Code extension, I found that this wasn't reliable for my workflow.

So I switched to the Colab Contents View for downloading files.


2. HTTP Server

I also experimented with:

from http.server import HTTPServer, SimpleHTTPRequestHandler
Enter fullscreen mode Exit fullscreen mode

and port 8000.

But this wasn't necessary.

The important lesson was:

Opening a port inside a remote cloud runtime doesn't automatically make localhost on your Windows machine point to that runtime.

Understanding where the server is actually running is essential.


📋 Quick Reference

Direction Method Destination
Local → Colab FileUpload widget /content/
Colab → Local Colab Contents View Windows PC
Local → Colab Google Drive /content/drive/
Colab → Local files.download() Not reliable in my VS Code workflow
Local ↔ Colab HTTP server Not necessary for this workflow

🔄 Complete Workflow

Uploading a dataset

Windows PC
     │
     ▼
FileUpload Widget
     │
     ▼
uploader.value
     │
     ▼
/content/
     │
     ▼
pd.read_csv()
Enter fullscreen mode Exit fullscreen mode

Downloading a generated file

Colab Runtime
     │
     ▼
/content/report.html
     │
     ▼
Colab Contents View
     │
     ▼
Windows PC
Enter fullscreen mode Exit fullscreen mode

🎯 Why This Was Useful for My ML Journey

This may look like a small file-management problem, but it helped me understand an important part of working with cloud environments.

When working with:

  • Google Colab
  • VS Code
  • Remote Jupyter kernels
  • Cloud VMs
  • Docker containers
  • Remote development environments

it's important to always ask:

Where is my code running?

and

Where does my file actually exist?

Once you understand that, file transfer becomes much easier to reason about.


📝 Final Takeaway

For my current VS Code + Google Colab workflow, I use:

📤 Local → Colab

import ipywidgets as widgets
from IPython.display import display

uploader = widgets.FileUpload(
    accept='*',
    multiple=True
)

display(uploader)
Enter fullscreen mode Exit fullscreen mode

Then:

for filename, fileinfo in uploader.value.items():
    with open('/content/' + filename, 'wb') as f:
        f.write(fileinfo['content'])
Enter fullscreen mode Exit fullscreen mode

📥 Colab → Local

Use:

Ctrl + Shift + P
        ↓
Colab: Focus on Contents View
        ↓
Find the file
        ↓
Download / Save
Enter fullscreen mode Exit fullscreen mode

I've also put both workflows into the handwritten cheat sheet attached to this article so that I can quickly refer back to them whenever I need to move files between my local machine and Colab.


🚀 Final Thought

A small debugging problem turned into a useful lesson about remote development, cloud runtimes, filesystems, and VS Code integrations.

I'm continuing to document these small problems and solutions as part of my 100 Days of Machine Learning journey.

If you're also using Google Colab through the VS Code Extension, I hope this helps!


#Python #GoogleColab #VSCode #MachineLearning #DataScience #Jupyter #100DaysOfML #LearningInPublic

Top comments (0)