Summary
In this video I walk through a hands‑on lesson on Python file input/output using a marine‑research scenario. I begin by asking students to download and unzip a provided archive that contains a Jupyter notebook, a captain's log (captain_log.txt), and a whale_sightings.csv file. The goal is to read, write, and append both unstructured text and structured CSV data, handle possible I/O errors, and combine the local datasets with an external CSV fetched via urllib.request.urlretrieve.
I cover the essential file‑handling concepts: the different file modes (r, w, a, x), the importance of using a with‑statement to ensure files close automatically, and the distinction between reading an entire file with read(), obtaining a list of lines with readlines(), and iterating line‑by‑line in a for‑loop. I demonstrate stripping newline characters, skipping empty or irrelevant lines (e.g., "sightings: None"), and using list slicing to extract useful portions of the log. For CSV handling I explain the csv module, delimiter selection, and how to parse the whale_sightings.csv into a list of records.
Next, I show how to write output files. I open a new file with 'w' to create a field_report.txt, write a header and a separator line ("="*40), and insert the count of expedition days that contained at least one sighting using an f‑string. I emphasize adding "\n" when writing strings and the difference between truncating with 'w' and appending with 'a'.
I also provide helper functions that take the combined datasets and produce bar charts of species counts, illustrating how visual summaries can be generated after data aggregation.
Finally, I remind students of administrative items: Quiz 1 and 2 are ungraded, the midterm is Friday, next week’s lab is cancelled, office hours are available, and any request for an early midterm must be emailed by the end of the day. Action items for students are to unzip the materials, complete the file‑I/O exercises, generate the field_report.txt and accompanying chart, and submit the report before the next class.