Python Data Foundations Documentation

A plain documentation-style guide for Python, data handling, visualization, and machine learning basics.

Local Files, Downloads, Drive, and Uploads

This page collects the file workflow commands from the notebook: writing files, downloading files, saving to Drive, and uploading files in Colab.

What you should be able to do
  • Write a text file with Python.
  • Download a file from a Colab runtime.
  • Upload files into a Colab session.
Reusable patterns
  • Use with open(...) to safely write files.
  • Colab file upload and download helpers are different from normal local Python file operations.

Listing 1. Save a text file locally

# save a text file locally
with open("example.txt", "w") as f:
  f.write("some content")

Listing 2. Download to the computer

# download to the computer
from google.colab import files
files.download("example.txt")
Expected text output or note
<IPython.core.display.Javascript object>

<IPython.core.display.Javascript object>

Listing 3. Save to drive

# save to Drive
with open("/content/drive/MyDrive/datoteka.txt", "w") as f:
  f.write("Example of writing to a file")

Listing 4. Upload files from the computer

# upload files from the computer
from google.colab import files
uploaded = files.upload() # files.upload opens an upload dialog

for fn in uploaded.keys():
  print(fn, len(uploaded[fn]))
Expected text output or note
<IPython.core.display.HTML object>

Back to overview