Command Palette

Search for a command to run...

Hectal
Phase 7Intermediate8 of 18 in Python

Errors, Files and Data Formats

Handle errors on purpose, read and write files safely, speak CSV, JSON, TOML and YAML, log like a professional, and debug with real tools.

Real programs meet things that go wrong: a file is missing, a number arrives as text, a network call fails. This phase teaches exceptions properly: how to catch them, how to raise your own, how to group several at once, and how context managers guarantee cleanup.

Then it moves to data that lives outside your program. You will read and write text files, build paths that work on every operating system, and convert data to and from the formats the world uses: CSV, JSON, TOML and YAML. You will also learn why one format, pickle, must never be opened from a stranger.

The phase ends with the two habits that make you effective in a real codebase: logging instead of print, and debugging with a method instead of by guessing. These are the skills behind every reliable AI pipeline that reads files, calls APIs and must keep going when one item fails.

0/12 · 0%
12 topics ~6 h 62 code blocks & diagrams
Start with the first topic
1
7.1

Exceptions: try, except, else, finally

An exception is Python's way of saying that something went wrong at this exact line. try/except lets you catch a specific kind of failure and carry on, else runs only when nothing failed, and finally always runs for cleanup.

28 min 4 code practice

2
7.2

Raising Errors and Custom Exception Classes

raise lets your own code signal a problem, raise ... from records the cause, and a small custom exception class gives callers a precise name to catch. add_note (Python 3.11) attaches extra context, and assert is for checking your own assumptions, not user input.

28 min 4 code practice

3
7.3

Exception Groups and except*

An ExceptionGroup bundles several unrelated errors into one exception, and except* handles the members of a group by type. They exist because concurrent code (like asyncio.TaskGroup) can fail in many places at once.

27 min 5 code practice

4
7.4

Context Managers and with

A with statement sets something up, runs your block, and then always cleans up, even if the block fails. Files, locks, database connections and temporary settings all use it, and any class with __enter__ and __exit__ can join in.

27 min 4 code practice

5
7.5

Reading and Writing Text Files

open() connects your program to a file. The mode says whether you read, write or append, the encoding says how characters become bytes (always pass encoding="utf-8"), and iterating over the file reads one line at a time without loading it all.

28 min 4 code practice

6
7.6

Paths the Modern Way: pathlib

pathlib.Path represents a file or folder location as an object. You join parts with /, ask questions like exists(), read and write with one call, search with glob, and your code works on Windows, macOS and Linux alike.

27 min 4 code practice

7
7.7

CSV and JSON

CSV stores tables as lines of comma-separated text and JSON stores nested data as text with objects, arrays, numbers and strings. The csv and json modules read and write them, and they are the two formats you will meet most in data and AI work.

29 min 6 code practice

8
7.8

TOML, YAML, Pickle and Binary Data

TOML and YAML are friendly formats for configuration files, pickle saves Python objects but must never be opened from an untrusted source, and struct and bytes let you read and write raw binary data.

30 min 6 code practice

9
7.9

Logging Instead of print

The logging module records what your program is doing with a severity level, a source name and a format, and can send the records anywhere. It replaces scattered print calls with output you can filter, store and search.

28 min 6 code practice

10
7.10

Debugging: breakpoint(), pdb and Reading State

Debugging is finding the first place where what your program holds differs from what you believed. You can do it with well-labelled prints, with breakpoint() and the pdb debugger, with your editor's debugger, and with a systematic method such as bisection.

31 min 6 code practice

11
7.11

Dates, Times and Time Zones

Python's datetime module has types for a date, a time of day, both together, and a length of time. Keep every moment as a time-zone-aware value, store and compare in UTC, show local time only at the edge, and write dates as ISO 8601 text.

40 min 1 diagram 6 code practice

12
7.12

XML and HTML: Reading Markup

XML and HTML describe data as a tree of nested elements with attributes and text. Python's xml.etree.ElementTree reads and writes XML, html.parser and BeautifulSoup read messy HTML, and any XML that comes from outside needs a hardened parser such as defusedxml.

37 min 1 diagram 5 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.