Skip to main content
← Python Study Guide

Unit 5 · Moderate

Files, APIs, and Automation

Where Python stops being an exercise and starts touching real data. The unit adds a new category of failure: the input may be missing, malformed, or unavailable, and correct code has to survive that.

What a strong answer looks like

A strong Unit 5 answer says what happens when the data is absent or the wrong shape, not only what happens when everything works.

Topics in this unit

1

Reading and Writing Files

Know

A context manager opens a file and guarantees it is closed even if an error occurs partway through.

Apply

Always open files with a context manager, and read line by line when the file may be large.

Watch out

Reading an entire large file into memory when streaming would do.

Study move

Read a file line by line, count matching lines, and confirm the file closes on an error.

2

Structured Formats

Know

CSV and JSON carry structure that plain text does not. Parsing with the right module avoids hand-splitting that breaks on quoted fields.

Apply

Use the CSV module rather than splitting on commas, because a quoted field may contain one.

Watch out

Splitting a CSV line on commas, which corrupts any row containing a quoted comma.

Study move

Parse a CSV row containing a quoted comma both ways and compare.

3

Consuming APIs

Know

An API returns structured data over the network, and both the request and the response can fail independently of your code being correct.

Apply

Check the response status before parsing, and handle the failure path explicitly.

Watch out

Assuming a successful response. Network calls fail routinely and silently corrupt downstream logic.

Study move

Describe every failure mode of one API call and what your code should do for each.

4

Error Handling and Automation

Know

Exception handling separates the expected path from the recovery path. Catching everything hides the bugs you needed to see.

Apply

Catch the specific exception you can actually recover from, and let the rest surface.

Watch out

A bare catch-all that swallows genuine errors and turns a crash into wrong output.

Study move

Write a read that handles a missing file specifically and lets other errors propagate.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Using context managers for every file
  • Parsing structured formats with their own modules
  • Catching specific exceptions rather than everything

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • HTTP libraries. The specific client library differs by course.
  • Scheduling. Automating on a timer is covered in some sections only.

Mastery checklist

  • Read a file safely with a context manager.
  • Parse CSV and JSON with the right module.
  • Check an API response before parsing it.
  • Catch a specific exception and explain why not a general one.

Check yourself

  • What does a context manager guarantee that a manual close does not?
  • Why does splitting a CSV line on commas fail?
  • What is wrong with catching every exception?

Modeling drill

Write a script that reads a CSV of student scores and reports the average, handling a missing file and a malformed row differently.

Context managerCSVJSONAPIStatus codeExceptionTraceback