Python (Fall 2026) Module 12-1: Introducing File Usage in Python
Watch on YouTube →
Overview
Bill Moseley introduces Python files as a way to preserve program data beyond a run, unlike variables that disappear when the program exits. He explains how `open()` connects a filename or path to a file object, then distinguishes read, append, write, and create modes while emphasizing the risk of overwriting important files.
Key takeaways
- Python variables do not persist after a program stops; writing data to a file on a hard drive lets a program use that data in later runs.
- `open()` returns a file object that acts as a connection to a file, rather than returning the file's contents.
- A filename alone refers to a local file, while a path provides directions to a file elsewhere on the computer.
- The `a` mode preserves existing contents and adds data at the end, while `w` replaces the existing contents.
- Python's exclusive-create mode, `x`, prevents accidental replacement by failing when the requested filename already exists.
- Scripts can modify files beyond their own folder, so use backups and restrict file access while learning.
Chapters
- Variables exist only while a Python program is running, so their data disappears when the program exits or encounters an error.
- Files store data on a hard drive, allowing applications such as an ATM or budget tracker to retain information between runs.
- Saving work to a file avoids the impractical alternative of keeping a program running continuously to protect its data.
- Python's `open()` function takes a filename and a mode, then returns a file object that connects the program to an external file.
- A file object is a connection or reference, not the file itself or its contents.
- A filename without a path looks for a file in the script's local folder; a full path specifies a file elsewhere on the computer.
- Full paths provide access to files throughout the computer, so Moseley recommends practicing with local files and being cautious about what a script can modify.
- Use `r` to read an existing file and `a` to add new content at the end without removing existing content.
- Use `w` to write new content, but note that it erases existing contents in that file.
- Use `x` for exclusive creation: it creates a new file but fails if a file with that name already exists.
- Back up sensitive files before experimenting with modes that can overwrite data.
Transcript excerpt
0:00 So, we are going to do something very exciting today. We are going to move into, or start moving into, the realm of making real programs that are actually useful. What I mean by that is we can finally start thinking about saving data that's produced by our programs in between using those programs. So, for example, the bank account, the ATM, right? Every time we restart that ATM, our balances go back to whatever they were at the start. That's not useful. That's not realistic. If it was a real bank account, or like an accounting software, or something that helped us keep track of our monthly budget and spending, like it has to save that data. Every, cuz even if we are like,
0:57 "No, no, I don't need to worry about saving. I'm just going to leave this running on my computer." At some point, you know, your dog's going to trip over the power cord and your computer's going to become unplugged, or there's going to be a power outage, or something will happen. And your computer will no longer be running, so you have to restart the program. And if you lose all your data at that point, it's going to be very frustrating. Right now, our capabilities are sort of like if Microsoft Word required you to sit down, write a whole paper without stopping, and then print it out before you left the computer lab. That's just not feasible, nor is it useful. But now, with the introduction of files, we will actually be able to save data in a more permanent way. Okay? So, the difference between saving data and not saving data is that right now what we've
1:57 been doing is just running a program that, that keeps the data in working memory. And when Python
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.