So far every value in your scripts has been hardcoded. Real scripts read input from somewhere — a file on disk, a URL, stdin. Today: files.
The open built-in opens a file. The with statement guarantees it gets closed:
with open("notes.txt") as f:
text = f.read()Inside the with block, f is a file handle. After the block exits (even if an error happens), Python closes the file automatically.
What's text?
A string — the entire file contents as one string, including newline characters. If you want one line at a time:
with open("notes.txt") as f:
for line in f:
print(line)Iterating the file handle yields one line per iteration. Memory-efficient — even 1GB files work, because only one line is in RAM at a time.
What if the file doesn't exist?
FileNotFoundError. Wrap in try / except FileNotFoundError if that's expected. Today's exercise creates the file first, so it's guaranteed to exist.
open — read text from diskwith open("notes.txt") as f:
text = f.read()open("notes.txt") opens for reading (default mode, "r").with ... as f: binds the file handle to f. When the block exits, Python closes the file.f.read() returns the entire contents as one string.with open("notes.txt") as f:
lines = f.readlines() # ['first line\n', 'second line\n', ...]Each element keeps its trailing \n. Use .strip() if you don't want it:
with open("notes.txt") as f:
lines = [line.strip() for line in f]with open("big_file.txt") as f:
for line in f:
process(line)For huge files, this is the right pattern. f.read() would load the whole thing into RAM.
Mode "w" opens for writing (truncates if it exists):
with open("out.txt", "w") as f:
f.write("hello\n")
f.write("world\n")Mode "a" appends without truncating.
| Mode | Meaning |
|---|---|
"r" | read text (default) |
"w" | write text (truncates) |
"a" | append text |
"rb" / "wb" | binary read/write |
Text mode handles newlines and decoding automatically. Binary mode gives raw bytes.
with# bad — file might not get closed if an error happens
f = open("notes.txt")
text = f.read()
f.close()
# good — always closed, even on error
with open("notes.txt") as f:
text = f.read()with is the canonical pattern for files in Python.
So far every value in your scripts has been hardcoded. Real scripts read input from somewhere — a file on disk, a URL, stdin. Today: files.
The open built-in opens a file. The with statement guarantees it gets closed:
with open("notes.txt") as f:
text = f.read()Inside the with block, f is a file handle. After the block exits (even if an error happens), Python closes the file automatically.
What's text?
A string — the entire file contents as one string, including newline characters. If you want one line at a time:
with open("notes.txt") as f:
for line in f:
print(line)Iterating the file handle yields one line per iteration. Memory-efficient — even 1GB files work, because only one line is in RAM at a time.
What if the file doesn't exist?
FileNotFoundError. Wrap in try / except FileNotFoundError if that's expected. Today's exercise creates the file first, so it's guaranteed to exist.
open — read text from diskwith open("notes.txt") as f:
text = f.read()open("notes.txt") opens for reading (default mode, "r").with ... as f: binds the file handle to f. When the block exits, Python closes the file.f.read() returns the entire contents as one string.with open("notes.txt") as f:
lines = f.readlines() # ['first line\n', 'second line\n', ...]Each element keeps its trailing \n. Use .strip() if you don't want it:
with open("notes.txt") as f:
lines = [line.strip() for line in f]with open("big_file.txt") as f:
for line in f:
process(line)For huge files, this is the right pattern. f.read() would load the whole thing into RAM.
Mode "w" opens for writing (truncates if it exists):
with open("out.txt", "w") as f:
f.write("hello\n")
f.write("world\n")Mode "a" appends without truncating.
| Mode | Meaning |
|---|---|
"r" | read text (default) |
"w" | write text (truncates) |
"a" | append text |
"rb" / "wb" | binary read/write |
Text mode handles newlines and decoding automatically. Binary mode gives raw bytes.
with# bad — file might not get closed if an error happens
f = open("notes.txt")
text = f.read()
f.close()
# good — always closed, even on error
with open("notes.txt") as f:
text = f.read()with is the canonical pattern for files in Python.
Create a free account to get started. Paid plans unlock all tracks.