open() — the first thing AI does in any real script
Pull rows from a CSV. Write a log line. Cache an API response. Save
the user's notes to disk. Read the prompt template. Every one of these
starts with the same call: open(). If you can't read the shape of a
file open, you can't read what almost any non-trivial AI-generated
script is doing in its first few lines.
The mental model
Treat a file like a phone call. There are exactly three moves:
- Pick up.
open(path, mode)connects your program to a file on disk and hands back a file object. - Talk. While the connection is open, you call methods on it:
f.read(),f.write(...), or iterate line-by-line withfor line in f:. - Hang up. When you're done, the connection has to be closed — otherwise the OS keeps the handle pinned, and on a long-running script those handles pile up until it refuses to open new ones.
Python wraps all three steps in one shape, the with block:
with open(path, mode) as f:
# do stuff with f
# file is closed here, automatically
The with line opens the file and binds it to f. The instant the
indent ends, Python closes the file for you — normal completion,
return, raised exception, every exit path runs the cleanup. The
alternative — bare f = open(path) … f.close() — is a manual
contract: if anything raises between the open and the close, the
close() never runs and the handle leaks. with makes that
impossible, which is why it's the only form worth shipping.
The mode argument is one short string
The second argument to open() controls what kind of access you get.
The four you'll see AI reach for, all the time:
"r"— read. The file must already exist. Default if you pass no mode."w"— write. Creates the file if it doesn't exist, truncates (empties) it if it does. This is how AI accidentally deletes your data."a"— append. Creates the file if needed, but adds to the end rather than overwriting. This is what you want for logs."rb"/"wb"— read/write binary. For images, audio, PDFs — any file that isn't text.
A worked example
The editor on the right has the canonical write-then-read pair:
with open("/tmp/note.txt", "w") as f:
f.write("hello from python\n")
with open("/tmp/note.txt") as f:
print(f.read())
open("/tmp/note.txt", "w")opens the file in write mode, creating it. Bind it tof.f.write(...)writes the string. The\nis a literal newline.- The
withblock ends, Python closes the file. - We open the same path with no mode (defaults to
"r"). f.read()returns the entire contents as one string. Print it.
Output:
hello from python
Pyodide (the in-browser Python this course runs on) gives us a real
virtual filesystem, so /tmp/note.txt behaves exactly the way it
would on your laptop — same open, same with, same modes.
Where AI specifically gets this wrong
Three patterns to flag in code Cursor writes:
-
Using
"w"when you meant"a". "Save the user's notes tonotes.txt" gets written asopen(path, "w"). Every time the user adds a note, the previous notes get erased. Always check the mode when AI writes a file the user expects to grow. -
Bare
open()withoutwith. Works on the happy path, leaks file handles the first time an exception slips between open and close. Wrap it inwith. Always. -
Reading a giant file with
.read()or.readlines(). Both load the entire file into memory — on a 5GB log, that's a crash.for line in f:streams one line at a time instead. (Remember to.strip()each line, or the kept\nplusprint's own newline double-spaces your output.)
Run the editor. We write hello from python to disk, then read it
back and print it. Two with blocks, two file handles, both cleanly
closed.