File I/O with the with Statement

Introduction

Up to now your programs have worked with data that lives only in memory: once the script ends, everything is gone. File input and output (file I/O) is how a program keeps data between runs — reading a configuration file, logging sensor readings, saving results to a text file that a colleague can open later. In Python the starting point is the built-in open() function, which takes a filename and a mode such as 'r' for reading, 'w' for writing (which overwrites), or 'a' for appending.

The catch is that an open file is a limited operating-system resource, and if your program crashes before closing it you can leak file handles or lose buffered data that was never written to disk. That is why Python offers the with statement, a context manager that automatically closes the file when the block ends — even if an exception is raised inside it. This connects directly to the error-handling material in this module: a with block paired with try/except is the standard, professional way to work with files. You should also get comfortable with pathlib, the modern object-oriented way to build and inspect file paths so your code works on Windows, macOS, and Linux without hard-coded slashes.

As you watch the videos below, pay attention to three things: the exact syntax of the with open(...) as f: line and what the variable f actually is; which methods are used to pull text out of a file (read, readline, readlines, or looping over the file directly); and how paths are constructed and checked before a file is opened. Try typing each example yourself and then deliberately break it — use a filename that does not exist — so you can see what error Python raises and how the with statement still cleans up.

Video 1: Python read a file 🔍

Bro Code · 3:40 · 135,412 views · 5y ago · captions — Watch on YouTube

About this video

A short, fast-paced walkthrough of reading a text file in Python using open() inside a with block, as shown in the description's try/with/print(file.read()) snippet. It demonstrates the basic file-reading pattern and wraps it in a try block so that a missing or unreadable file is handled gracefully rather than crashing the program.

Why this video

Bro Code is a well-known Python tutorial channel with concise, code-first examples, and this clip hits the core of the topic — opening files, the with statement context manager, and reading text — while explicitly tying file I/O to error handling, which is exactly the focus of this module.

Video 2: Intro to Python Pathlib Library | Automate Everything with Python

Learn With Udemy Course · 9:27 · 70 views · 3y ago · captions — Watch on YouTube

About this video

An introduction to Python's pathlib library, the modern object-oriented approach to working with file and directory paths. Expect coverage of creating Path objects, joining path components, and inspecting paths and files, presented as part of a broader automation-with-Python course.

Why this video

It directly addresses the 'Working with File Paths Using pathlib' subtopic, giving you the cross-platform path handling you need before opening files, and at roughly nine minutes it is long enough to show real examples rather than just syntax.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.