In [ ]:
# Initialize Otter
import otter
grader = otter.Notebook("08_lab_file_io.ipynb")

Lab 08: Coral Reef Health Survey

You're a marine biologist processing field data from reef survey dives along the Florida Keys. Your dive team has collected two types of data:

  • Dive notes — raw text observations from the field
  • Survey logs — structured CSV records of coral colonies across multiple reef sites

In this lab, you'll write Python code to read, process, and save this data — the same workflow used by real scientists when handling field data.

Run every cell in order from top to bottom. Later cells depend on earlier ones.


In [ ]:
# Validation: Check Python version and file location
import os
import sys

# Supported Python versions for this course
SUPPORTED_VERSIONS = ["3.12", "3.13"]
current_version = f"{sys.version_info.major}.{sys.version_info.minor}"

if current_version not in SUPPORTED_VERSIONS:
    raise EnvironmentError(
        f"Python version mismatch!\n"
        f"Supported: {', '.join(SUPPORTED_VERSIONS)}\n"
        f"Found: {current_version}\n"
        f"Please use Python 3.12 or 3.13 for this course."
    )
print(f"Python version: {current_version}")

# Expected filename for this assignment
EXPECTED_FILE = "08_lab_file_io.ipynb"

# Check file is in the correct directory
current_path = os.path.abspath("")
required_path_segment = os.path.join("Documents", "CSC115", "lab_notebooks")
allowed_envs = ["submission", "dist", "live", "source"]
in_allowed_env = any(env in current_path for env in allowed_envs)

if required_path_segment not in current_path and not in_allowed_env:
    raise EnvironmentError(
        f"File location error!\n"
        f"This notebook should be in: ~/Documents/CSC115/lab_notebooks/\n"
        f"Current location: {current_path}\n"
        f"Please move this file to the correct directory."
    )

# Verify filename (skip in grading/development environments)
if not in_allowed_env:
    if not os.path.exists(EXPECTED_FILE):
        notebooks = [f for f in os.listdir(".") if f.endswith(".ipynb")]
        raise EnvironmentError(
            f"Filename mismatch!\n"
            f"Expected: {EXPECTED_FILE}\n"
            f"Found notebooks: {notebooks}\n"
            f"Please rename your notebook to '{EXPECTED_FILE}'."
        )

print(f"File location: {os.path.join(current_path, EXPECTED_FILE)}")
In [ ]:
import csv

# ── Reef survey column indices ──────────────────────────────────────────
# Each row in reef_survey.csv has these columns, in this order:
# site, date, species, health_status, colony_count, depth_m
SITE    = 0
DATE    = 1
SPECIES = 2
HEALTH  = 3
COUNT   = 4
DEPTH   = 5

print("Setup complete.")

Section 1: Text Files

Text files store unstructured data — field notes, logs, reports. Python's built-in open() function lets you read them line by line.

Key pattern:

with open("filename.txt", "r") as file:
    lines = file.readlines()  # returns a list of strings

The with statement closes the file automatically when the block ends. Each line in lines ends with \n — use .strip() to remove it.


Q1: Read Dive Notes

The file dive_notes.txt contains field notes from a recent dive at Carysfort Reef. Read the file and extract the site name.

✏️ Your tasks:

  1. Read dive_notes.txt using a with statement and .readlines(). Store the list of lines in dive_lines.
  2. Extract the dive site name from the first line (everything after "Dive Site: ") and store it in dive_site. Strip any trailing whitespace.
In [ ]:
# ✏️ YOUR ANSWER HERE

...
    dive_lines = ...

dive_site = ...
In [ ]:
grader.check("q1_read_dive_notes")

Q2: Write a Summary File

Now that you have the dive notes loaded, count how many lines mention coral and save a short summary to a new file.

Key pattern for writing:

with open("output.txt", "w") as file:
    file.write("some text\n")  # \n adds a newline at the end

✏️ Your tasks:

  1. Count how many lines in dive_lines contain the word "coral" (case-insensitive). Store the count in coral_mentions.
  2. Write a file called dive_summary.txt with exactly two lines using dive_site for the site name and coral_mentions for the count. Use the format Site: <name> and Coral mentions: <count>.
In [ ]:
# ✏️ YOUR ANSWER HERE

coral_mentions = ...
...
    ...
        ...

...
    ...
    ...
In [ ]:
grader.check("q2_write_summary")

Section 2: CSV Files

CSV (Comma-Separated Values) files store structured data in rows and columns. Python's csv module makes reading them straightforward.

Key pattern:

import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    next(reader)  # skip the header row
    for row in reader:
        print(row)  # each row is a list of strings

Important: Every value from a CSV file is a string — even numbers. You must convert with int() or float() before doing math.


Q3: Read the Reef Survey

The file reef_survey.csv contains coral colony observations from four reef sites. Read it into a list of lists.

✏️ Your tasks:

  1. Read reef_survey.csv using csv.reader inside a with block. Skip the header row with next(reader). Store each data row in a list called survey_data.
  2. Store the total number of observations in total_observations.
In [ ]:
# ✏️ YOUR ANSWER HERE

survey_data = ...
...
    reader = ...
    ...
    ...
        ...

total_observations = ...
In [ ]:
grader.check("q3_read_csv")

Q4: Filter and Aggregate

Now that you have all survey data loaded, find the healthy coral observations.

Remember: CSV values are strings. To add colony counts, you must convert: int(row[COUNT]).

✏️ Your tasks:

  1. Create a list called healthy_corals containing only rows where the health status is "healthy".
  2. Calculate total_healthy_colonies by summing the colony counts across all healthy rows. Convert each count to int before adding.
In [ ]:
# ✏️ YOUR ANSWER HERE

healthy_corals = ...
total_healthy_colonies = ...

...
    ...
        ...
        ...
In [ ]:
grader.check("q4_filter_aggregate")

Section 3: Writing CSV Files

csv.writer writes lists to a CSV file — one list per row.

Key pattern:

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["col1", "col2"])  # header
    for row in data:
        writer.writerow([row[0], row[2]])  # select specific columns

Q5: Write the Healthy Coral Report

Write the filtered healthy coral data to a new CSV file with only three columns.

✏️ Your task: Write healthy_corals to healthy_report.csv using csv.writer. Include a header row with columns: site, species, colony_count. Write only those three columns from each row — not all six.

In [ ]:
# ✏️ YOUR ANSWER HERE

...
    writer = ...
    ...
    ...
        ...
In [ ]:
grader.check("q5_write_csv")

Stretch Goal (Bonus): Site Summary Function

If you've finished Q1-Q5, try this bonus challenge.

✏️ Your task: Write a function called site_summary(survey_data, site_name) that:

  1. Takes the full survey_data list and a site_name string
  2. Filters to rows matching that site
  3. Counts the number of observations
  4. Sums the total colony count (remember: convert to int!)
  5. Returns a formatted string like: "Carysfort Reef: 3 observations, 24 total colonies"
In [ ]:
# ✏️ YOUR ANSWER HERE

...
    site_rows = ...
    obs_count = ...
    total_colonies = ...
    ...
In [ ]:
grader.check("q_stretch_site_summary")

Summary

Concept Syntax What it does
Open for reading open("file.txt", "r") Opens a file to read
Open for writing open("file.txt", "w") Creates/overwrites a file
Context manager with open(...) as file: Closes file automatically
Read all lines file.readlines() Returns list of strings (each ends with \n)
Write a line file.write("text\n") Writes text; include \n for newlines
Read CSV csv.reader(file) Iterates rows as lists of strings
Skip header next(reader) Advances past the first row
Write CSV csv.writer(file) Writes lists as comma-separated rows
Write a row writer.writerow([...]) Writes one row to CSV
Convert string int("12"), float("8.5") CSV values are strings — convert before math

Submitting to Gradescope

This file is saved on your computer — you do not need to download it from Jupyter.

  1. Find the file on your computer:

    • Mac: Open Finder → Documents/CSC115/lab_notebooks/08_lab_file_io/08_lab_file_io.ipynb
    • Windows: Open File Explorer → Documents\CSC115\lab_notebooks\08_lab_file_io\08_lab_file_io.ipynb
  2. Upload to Gradescope:

    • Go to gradescope.com and open this assignment
    • Click "Submit" and upload the .ipynb file
    • The filename must be exactly 08_lab_file_io.ipynb

Do NOT use File > Download in Jupyter — this can rename the file and cause submission errors.


You have completed the File IO and Context Manager notebook.


To double-check your work, the cell below will rerun all of the autograder tests.

In [ ]:
grader.check_all()