In Foundations you saw open(...) as f: once. Today we slow down on what open actually returns and what you can do with it.
f = open("numbers.txt")
text = f.read()
f.close()open returns a file handle — not the contents. f.read() is a separate step that pulls the bytes out.
Right. open doesn't read anything; it sets up a connection to the file. .read() reads it all into one string. .close() releases the OS resource.
Why bother showing the manual open / close if we always use with?
So you understand what with is doing for you — closing the file automatically at block exit. We'll see with properly in lesson 4. Today: notice the shape — open the file, read it, get a single string back, then split or process that string.
And f.read() returns the whole file. What's in the string?
Exactly the bytes the file contains, decoded as text. If the file has 5 lines, you get one string with 4 newline characters (\n) inside it. To count lines, split on \n — or use .splitlines() which is purpose-built for that.
open — connect to a filef = open("data.txt")Returns a file object — a handle. Default mode is "r" (read text). The file is not read yet; you've just opened a connection.
.read() — pull all contents into a stringtext = f.read() # entire file as one string.close() — release the handlef.close()In well-behaved code you always close. In the next two lessons you'll see why with is the better way; for today, manual close shows the lifecycle.
Two equivalent shapes:
text = f.read()
lines = text.splitlines() # ['line 1', 'line 2', 'line 3']
print(len(lines)) # 3.splitlines() handles \n, \r\n, and \r and drops the trailing newlines. Cleaner than text.split("\n") (which leaves a trailing empty string when the file ends in a newline).
| Mode | Meaning |
|---|---|
"r" | read text (default) |
"w" | write text — truncates first |
"a" | append text |
"rb" / "wb" | binary read / write |
Next lesson covers writing. Lesson 4 covers with. Today: open, read, count.
Create a free account to get started. Paid plans unlock all tracks.
In Foundations you saw open(...) as f: once. Today we slow down on what open actually returns and what you can do with it.
f = open("numbers.txt")
text = f.read()
f.close()open returns a file handle — not the contents. f.read() is a separate step that pulls the bytes out.
Right. open doesn't read anything; it sets up a connection to the file. .read() reads it all into one string. .close() releases the OS resource.
Why bother showing the manual open / close if we always use with?
So you understand what with is doing for you — closing the file automatically at block exit. We'll see with properly in lesson 4. Today: notice the shape — open the file, read it, get a single string back, then split or process that string.
And f.read() returns the whole file. What's in the string?
Exactly the bytes the file contains, decoded as text. If the file has 5 lines, you get one string with 4 newline characters (\n) inside it. To count lines, split on \n — or use .splitlines() which is purpose-built for that.
open — connect to a filef = open("data.txt")Returns a file object — a handle. Default mode is "r" (read text). The file is not read yet; you've just opened a connection.
.read() — pull all contents into a stringtext = f.read() # entire file as one string.close() — release the handlef.close()In well-behaved code you always close. In the next two lessons you'll see why with is the better way; for today, manual close shows the lifecycle.
Two equivalent shapes:
text = f.read()
lines = text.splitlines() # ['line 1', 'line 2', 'line 3']
print(len(lines)) # 3.splitlines() handles \n, \r\n, and \r and drops the trailing newlines. Cleaner than text.split("\n") (which leaves a trailing empty string when the file ends in a newline).
| Mode | Meaning |
|---|---|
"r" | read text (default) |
"w" | write text — truncates first |
"a" | append text |
"rb" / "wb" | binary read / write |
Next lesson covers writing. Lesson 4 covers with. Today: open, read, count.