---
# System prepended metadata

title: 08 - Files

---

# 08 - Files

**Notebook:** `08-files.ipynb`

## Video

<iframe width="720" height="406" src="https://www.youtube.com/embed/zMWmWN88F3M?si=kAftyBdYekgpeY2k" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

---

## A Note About This Section

This is one of the few things that is actually **harder in Colab** than in other coding environments. It's because we're working with the quirkiness of Google Drive. I apologize in advance—it's just a little bit of a pain.

We're learning less about code here and more about the jankiness of a platform's quirks. But we've got to do it for this to work and for us to be able to hold onto all the stuff we create.

---

## Step 1: Mount Google Drive

The first cell under Colab Setup will mount your Google Drive. It's going to ask you if it can have permission—just click yes.

```python
from google.colab import drive
drive.mount('/content/drive')
```

When prompted:

1. Click "Connect to Drive"
2. Click "Continue"
3. Grant access

Once you do this, you'll be able to:

- **Save things** to your Google Drive from the notebook
- **Access things** in your Google Drive from the notebook

This becomes important because we're going to work on an inventory of poems and we don't want to have to type all of that in as variables. It doesn't make sense if we already have text files—and some text files (like a Shakespeare play) are way too long to put in as a variable.

---

## Step 2: Find the Folder Panel

Once you've connected and hit reload a couple times, you should see:

1. Click the **folder icon** in the left sidebar
2. You'll see `sample_data` (already there)
3. `drive` should appear after you've connected
4. Navigate to `My Drive` → wherever you put your tutorial folder

---

## Step 3: Copy Paths

This is how your code is going to find where things are.

When you click on the **three dots** next to a folder or file, you get options:

- Add new files
- Rename folder
- Delete folder
- **Copy path** ← This is what we want

For example, copy the path to your `output` folder:

```text
/content/drive/MyDrive/bird-poem-tutorial-sequence/output
```

---

## Step 4: Set Up Your Directories

Paste the paths into your code:

```python
from pathlib import Path

OUTPUT_DIR = Path("/content/drive/MyDrive/bird-poem-tutorial-sequence/output")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

TEXTS_DIR = Path("/content/drive/MyDrive/bird-poem-tutorial-sequence/texts")

print(f"Outputs will be saved to: {OUTPUT_DIR}")
```

The `mkdir(parents=True, exist_ok=True)` part:

- Creates the directory if it doesn't already exist
- Won't error if it already exists

---

## Reading Text Files

```python
def read_text_files(folder: Path) -> list[dict]:
    """Read all .txt and .md files from a folder."""
    files = []
    for filepath in sorted(folder.glob("*")):
        if filepath.suffix in [".txt", ".md"]:
            files.append({
                "filename": filepath.name,
                "text": filepath.read_text()
            })
    return files

source_files = read_text_files(TEXTS_DIR)
print(f"Loaded {len(source_files)} files")
```

This function:

1. Goes through all files in the folder
2. Finds ones ending in `.txt` or `.md`
3. Reads their content
4. Returns a list of dictionaries with filename and text

---

## Writing Files

### Writing Text

```python
output_path = OUTPUT_DIR / "results.txt"
output_path.write_text("Analysis complete!")
```

### Writing JSON

```python
import json

data = {"title": "The Raven", "bird": "raven"}
with open(OUTPUT_DIR / "poem.json", "w") as f:
    json.dump(data, f, indent=2)
```

### Reading JSON

```python
with open(OUTPUT_DIR / "poem.json", "r") as f:
    loaded = json.load(f)
```

---

## Try It

1. Make sure your Drive is mounted
2. Navigate to your tutorial folder in the file browser
3. Copy the paths for your `texts` and `output` folders
4. Try reading a text file and printing its contents
5. Try writing some text to a new file

---

## Summary

In this notebook, you learned:

- **Mount Google Drive** to access your files in Colab
- Use `Path` to work with file paths
- **Read text files** into your code
- **Write files** to save your output
- Work with **JSON** for structured data storage

Next up: **the encyclopedia** — putting it all together!