Module 13 · File Handling — topic 6 of 15
File modes
Every open() mode explained: r, w, a, x, the + update modes, text versus binary (t and b), what each does to existing files, and choosing the safest mode.
Learning objectives
- Choose the correct mode for a task
- Explain what each mode does to existing files
- Use binary and update modes
Definition
The mode string passed to open() decides whether the file is read, written, appended or created, whether existing content is kept, and whether data is text (str) or binary (bytes).
Introduction
One letter in open() decides whether your file is read, extended or wiped. Knowing the modes by heart prevents painful accidents.
Why it matters
Picking the right mode expresses intent and protects data: "x" refuses to overwrite, "a" never erases, "r" can't modify.
Detailed explanation
"r" — read (default). The file must exist. Position starts at the beginning.
"w" — write. Creates the file or truncates it to empty.
"a" — append. Creates the file if needed; writes always go to the end.
"x" — exclusive creation. Creates a new file and fails with FileExistsError if it already exists.
Adding + allows both reading and writing: "r+" (file must exist, no truncation), "w+" (truncates), "a+" (writes at the end).
Adding b selects binary mode ("rb", "wb", "ab"): data is bytes and no encoding or newline translation happens. t (text) is the default.
With update modes, f.seek(offset) moves the position and f.truncate() cuts the file at the current position.
Syntax
- r, w, a, x choose the action.
- + adds the other direction.
- b switches to bytes.
Code examples
"x" refuses to overwrite
x refuses to overwrite created
Updating in place with "r+"
3
More examples
Copying a binary file in chunks
10240 3 True
Step-by-step explanation
- Binary modes read and write bytes exactly, which is essential for images, archives and other non-text files.
- Reading 4,096 bytes at a time keeps memory use small, even for huge files.
- The walrus operator stops the loop when read returns empty bytes at the end of the file.
Expected output
Running “Copying a binary file in chunks” prints exactly:
Practical applications
- "x" for creating output files without clobbering previous results.
- "rb"/"wb" for images, PDFs and downloads.
Best practices
- Use the least powerful mode that does the job.
- Use "x" when overwriting would be a mistake.
- Use binary mode for anything that isn't text.
Common mistakes
"w+" to read existing content
Avoid
Prefer
"w+" truncates first, so there is nothing to read.
Text mode for binary data
Avoid
Prefer
Decoding image bytes as text fails or corrupts the data.
Important notes
- shutil.copyfile performs chunked copying for you.
Key takeaways
- r reads, w truncates, a appends, x creates new.
- + adds reading or writing.
- b switches to bytes.
Try it yourself
- 1Write a function that saves output to a new file name (out1.txt, out2.txt, …) using "x" to avoid overwriting.
- 2Compare two binary files chunk by chunk and report whether they are identical.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhich mode creates a new file but fails if the file already exists?