🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

File Modes

AI & DATA SCIENCE // file-modes

A file mode is the string passed as open()'s second argument that determines whether a file is opened for reading, writing, or appending, and whether it's treated as text or binary.

Syntax

open(path, "r")   # read text (default)
open(path, "w")   # write text, overwriting
open(path, "a")   # append text
open(path, "rb")  # read binary
open(path, "x")   # exclusive creation

Deep Dive Course

The mode string combines a primary mode — 'r' for reading, the default, which fails if the file doesn't exist; 'w' for writing, which creates the file if missing and truncates/overwrites it if it already exists; 'a' for appending, which creates the file if missing and always writes at the end; and 'x' for exclusive creation, which fails if the file already exists, useful for avoiding accidental overwrites — with an optional suffix, a 'b' for binary mode, returning/accepting bytes instead of str, or a '+' for allowing both reading and writing through the same file object.

1Understanding File Modes

The mode string combines a primary mode — 'r' for reading, the default, which fails if the file doesn't exist; 'w' for writing, which creates the file if missing and truncates/overwrites it if it already exists; 'a' for appending, which creates the file if missing and always writes at the end; and 'x' for exclusive creation, which fails if the file already exists, useful for avoiding accidental overwrites — with an optional suffix, a 'b' for binary mode, returning/accepting bytes instead of str, or a '+' for allowing both reading and writing through the same file object.

💡

'w' mode silently truncates an existing file to zero length the instant you open it, even before you write anything — if you meant to add to an existing file instead of erasing it, you wanted 'a', not 'w'.

editor.html
with open("draft.txt", "w") as f:
    f.write("First version")

with open("draft.txt", "w") as f:
    f.write("Overwritten")

with open("draft.txt", "r") as f:
    print(f.read())
localhost:3000

2Practical Example

Here is a real-world application of File Modes showing how it is used in production Python code.

editor.html
with open("log.txt", "w") as f:
    f.write("Entry 1\n")

with open("log.txt", "a") as f:
    f.write("Entry 2\n")

with open("log.txt", "r") as f:
    print(f.read())
localhost:3000

3Best Practices

Follow these guidelines when working with File Modes:

1. Double check 'w' versus 'a' before opening a file you don't want to accidentally erase — 'w' truncates immediately on open

2. Use 'x' instead of 'w' when you specifically want the open() call to fail rather than silently overwrite an existing file

3. Use a 'b' suffix, like 'rb' or 'wb', for non-text data — images, audio, arbitrary binary formats — instead of trying to force it through text mode

⚠️

Tip: 'w' mode silently truncates an existing file to zero length the instant you open it, even before you write anything — if you meant to add to an existing file instead of erasing it, you wanted 'a', not 'w'.

editor.html
with open("draft.txt", "w") as f:
    f.write("First version")

with open("draft.txt", "w") as f:
    f.write("Overwritten")

with open("draft.txt", "r") as f:
    print(f.read())
localhost:3000

Examples

Example 01Basic Usage
with open("draft.txt", "w") as f:
    f.write("First version")

with open("draft.txt", "w") as f:
    f.write("Overwritten")

with open("draft.txt", "r") as f:
    print(f.read())
Example 02Advanced Example
with open("log.txt", "w") as f:
    f.write("Entry 1\n")

with open("log.txt", "a") as f:
    f.write("Entry 2\n")

with open("log.txt", "r") as f:
    print(f.read())

Best Practices

  • Double check 'w' versus 'a' before opening a file you don't want to accidentally erase — 'w' truncates immediately on open
  • Use 'x' instead of 'w' when you specifically want the open() call to fail rather than silently overwrite an existing file
  • Use a 'b' suffix, like 'rb' or 'wb', for non-text data — images, audio, arbitrary binary formats — instead of trying to force it through text mode

Interview Question

What's the practical difference between opening a file with 'w' mode versus 'a' mode, if the file already exists?

Hint: Think about what happens to existing content the moment the file is opened.

'w' mode immediately truncates the existing file to zero bytes the moment it's opened, discarding all previous content, even before any write() call happens, so any prior data in that file is lost as soon as you open it this way. 'a' mode instead opens the file without touching its existing content, and every subsequent write() is added at the end of the file, preserving whatever was already there.

Exercises

MediumPractice using File Modes in a real scenario.
View Solution
with open("draft.txt", "w") as f:
    f.write("First version")

with open("draft.txt", "w") as f:
    f.write("Overwritten")

with open("draft.txt", "r") as f:
    print(f.read())

Frequently Asked Questions

What's the practical difference between opening a file with 'w' mode versus 'a' mode, if the file already exists?

'w' mode immediately truncates the existing file to zero bytes the moment it's opened, discarding all previous content, even before any write() call happens, so any prior data in that file is lost as soon as you open it this way. 'a' mode instead opens the file without touching its existing content, and every subsequent write() is added at the end of the file, preserving whatever was already there.

Related Functions

open()write()read()