Google Colab
file handling
Python
data processing
cloud computing

Write out file with google colab

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Writing files in Google Colab is straightforward, but durability depends on where you save them. Files written under /content are ephemeral and can disappear when the runtime restarts. For persistent output, you should write to mounted Google Drive or explicitly download artifacts.

Understand Colab Storage Layers

Colab notebooks commonly use two storage locations:

  • Runtime disk under /content.
  • Mounted Drive under /content/drive.

Runtime disk is fast and ideal for temporary intermediate files. Drive is persistent across sessions but usually slower for heavy write workloads.

You should decide location based on whether the output must survive runtime disconnects.

Write a Simple Text File to Runtime

Use standard Python file APIs for quick output during one session.

python
1from pathlib import Path
2
3out = Path("/content/result.txt")
4out.write_text("hello from colab\n", encoding="utf-8")
5
6print(out.exists(), out)

This method is useful for logs, debug snapshots, or temporary exports you will process immediately.

Write Structured Files with Pandas

For tabular outputs, pandas provides clear and reliable writers.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Ava", "Noah", "Mia"],
5    "score": [95, 88, 91],
6})
7
8csv_path = "/content/scores.csv"
9df.to_csv(csv_path, index=False)
10print("saved:", csv_path)

The file appears in the Colab file sidebar and can be downloaded manually.

Persist Files to Google Drive

Mounting Drive is the standard way to keep generated files beyond runtime lifetime.

python
from google.colab import drive

drive.mount('/content/drive')

Then write to a folder in MyDrive.

python
1from pathlib import Path
2
3base = Path("/content/drive/MyDrive/colab_outputs")
4base.mkdir(parents=True, exist_ok=True)
5
6(base / "report.txt").write_text("persistent output\n", encoding="utf-8")
7print("saved to", base)

If you rerun the notebook later, the file still exists in Drive.

Download Output to Local Machine

When you need quick local export, use Colab download helpers.

python
from google.colab import files

files.download('/content/scores.csv')

This triggers browser download and is practical for occasional small files.

Handle Large Outputs Safely

For large writes, stream incrementally and avoid keeping huge strings in memory.

python
with open('/content/large.log', 'w', encoding='utf-8') as fh:
    for i in range(100000):
        fh.write(f"row {i}\n")

For large datasets destined for Drive, write to runtime first, then copy finalized artifacts. This often performs better than writing directly to Drive on every small operation.

python
!cp /content/large.log /content/drive/MyDrive/colab_outputs/

Create Compressed Artifacts for Easy Sharing

When notebooks produce many files, package them into one archive.

python
1import zipfile
2from pathlib import Path
3
4folder = Path("/content/artifacts")
5folder.mkdir(exist_ok=True)
6(folder / "metrics.txt").write_text("accuracy=0.93\n", encoding="utf-8")
7(folder / "notes.txt").write_text("run finished\n", encoding="utf-8")
8
9zip_path = Path("/content/artifacts.zip")
10with zipfile.ZipFile(zip_path, "w") as zf:
11    for p in folder.iterdir():
12        zf.write(p, arcname=p.name)
13
14print("archive:", zip_path)

A zip artifact is easier to download and track than multiple loose files.

Verify Paths to Avoid Silent Mistakes

Long notebooks can change context and make path bugs hard to spot. Add simple checks before writing important output.

python
1import os
2
3print("cwd:", os.getcwd())
4print("drive mounted:", os.path.exists('/content/drive/MyDrive'))

Use absolute paths for critical files so behavior is consistent across notebook reruns.

Practical Persistence Strategy for Training Runs

For long ML jobs, save progress incrementally:

  • Write checkpoints at intervals.
  • Append metrics to a log file on Drive.
  • Keep a small metadata file with run parameters.

This strategy reduces data loss risk when runtimes disconnect unexpectedly.

Common Pitfalls

  • Saving critical output only under /content and losing it after runtime reset.
  • Forgetting to mount Drive before writing Drive paths.
  • Using relative paths that depend on changing working directory.
  • Writing huge files directly to Drive in many tiny operations.
  • Generating many artifacts but not packaging or naming them consistently.

Summary

  • Use /content for temporary fast local runtime files.
  • Use mounted Drive for persistence across Colab sessions.
  • Use files.download for quick local exports.
  • Prefer absolute paths and explicit path checks in long notebooks.
  • Save important outputs incrementally to reduce runtime disconnect risk.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.