Python (Fall 2026) Module 12-2: Reading and Writing Files
Overview
Bill Moseley explains how Python file objects read text with `read()` and `readline()`, and how their pointer advances as content is consumed. He also demonstrates writing with modes such as `w`, `a`, and `x`, emphasizing that the chosen mode and file state affect whether data is replaced, appended, or read successfully.
Key takeaways
- `read()` without an argument returns the entire file as a string, while `read(n)` returns up to `n` characters, including spaces and line breaks.
- `readline()` reads one line at a time and advances the file object's pointer, so each call normally retrieves the next line.
- Python's `write()` method does not add line breaks automatically; include `\n` when appended text should begin on a new line.
- Opening mode determines how writes affect a file: `w` replaces or creates, `a` appends, and `x` creates only when the path is new.
- To switch from appending to reading, close the file and reopen it with a reading mode rather than attempting to read through the append-opened object.
Chapters
- Opening `intro.txt` creates a file object, but retrieving its contents requires a read method.
- `fiIntro.read()` returns the whole file as a string; `fiIntro.read(30)` returns up to the first 30 characters.
- Spaces, tabs, and newline characters count toward the requested character total, and newlines preserve the file's displayed structure.
- `fiIntro.readline()` reads one line, and a subsequent call starts at the next line because the file object tracks its position with a pointer.
- Repeated `readline()` calls can process a text file line by line, with the pointer moving forward after each read.
- The file's current position and how it was opened affect what later read operations return.
- The examples distinguish `w` for replacing or creating a file, `a` for appending, and `x` for creating a file only if it does not already exist.
- `write()` adds exactly the supplied string; to start appended text on a new line, include a newline character such as `\n`.
- After opening a file for appending, close it and reopen it for reading before attempting to read its contents.
- Choosing the wrong mode or using a file in the wrong state can damage data or prevent the intended operation.
Transcript excerpt
0:01 So if I want to read from a file, now remember, this only creates a file object. This doesn't get me what's in that file yet. If I want to get what's in the file, I have to read the file. And there's a few different ways to do this. Okay. All of them involve using a read method of the file object. So this would be a file object that I already opened and attached to a file. If I do this, myFile.read with no arguments, it's going to take the entire contents of that file and put them as a string into that strFileText variable. It's going to take everything. If I give it a numeric parameter, you can see it's an integer because we can't do fractional characters. What that means is you're going to read the first 10 characters of that file. Then, okay, the
1:10 next one, read line, will read one line from the file at a time. So let me show you how this looks. Okay. So, let's say I have this file here. Where did I put that? You know what's helpful? If you're 50 years old and half blind, wearing glasses. That's nice. There we go. Now I can see. Okay. So, I've got this intro.txt file here. And this is just, you can see, five lines of plain text. It's not code. It's not anything special. But if I want to play around with that, I'm just going to call this
2:04 fi, or fiIntro. So I know it's a file object, and I'm going to open that. So I'm going to just say intro.txt for reading. Okay. And so now if I say strText = read with no parameters, and I print that out, str. Let's run that. Okay. So it just takes everything that's in that and prints it out, including the
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
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.