file handling
text mode
Python programming
read and write files
file operations

Open files in 'rt' and 'wt' modes

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, rt means “read text” and wt means “write text.” They are just explicit versions of the normal text-file modes. Understanding them matters because text mode does newline translation and encoding-aware string handling, while write mode also truncates the target file.

What the Mode Letters Mean

Python file modes are built from small pieces:

  • 'r means open for reading'
  • 'w means open for writing'
  • 't means treat the file as text'
  • 'b means treat the file as binary'

So these are equivalent:

python
open("notes.txt", "r")
open("notes.txt", "rt")

Text mode is already the default, so the t is mostly for explicitness.

Reading with rt

Use rt when you want Python strings, not raw bytes.

python
1with open("notes.txt", "rt", encoding="utf-8") as f:
2    content = f.read()
3
4print(content)

In text mode, Python decodes bytes using the specified encoding and returns str values.

Text mode also normalizes line endings. For example, Windows line endings such as \r\n are read as \n in normal text mode.

Writing with wt

Use wt when you want to write text strings to a file.

python
with open("notes.txt", "wt", encoding="utf-8") as f:
    f.write("Hello\n")
    f.write("World\n")

Two important details:

  • the file is opened for writing only
  • if the file already exists, it is truncated first

That truncation behavior is the part developers most often forget.

Why Encoding Should Usually Be Explicit

If you do not supply encoding, Python uses a platform-dependent default. That can make behavior vary between machines.

For predictable text handling, prefer this style:

python
with open("data.txt", "rt", encoding="utf-8") as f:
    print(f.readline())

and:

python
with open("data.txt", "wt", encoding="utf-8") as f:
    f.write("example\n")

Being explicit avoids confusing bugs with accented characters, symbols, and cross-platform scripts.

wt Versus at

If you want to add text without erasing the existing file, wt is the wrong mode. Use append mode instead.

python
with open("log.txt", "at", encoding="utf-8") as f:
    f.write("new line\n")

That difference is critical:

  • 'wt overwrites from the start after truncating'
  • 'at appends to the end'

Text Mode Versus Binary Mode

Text mode returns strings and performs encoding and newline handling. Binary mode returns raw bytes.

python
1with open("image.png", "rb") as f:
2    header = f.read(8)
3
4print(header)

That is why rt and wt are appropriate for text files, not images, archives, or arbitrary binary formats.

Error Behavior You Should Expect

rt raises an error if the file does not exist, because reading assumes there is already something to open.

python
1try:
2    with open("missing.txt", "rt", encoding="utf-8") as f:
3        print(f.read())
4except FileNotFoundError as exc:
5    print(exc)

By contrast, wt creates the file if it is missing. That difference is useful, but it also means wt can overwrite a path you did not intend to replace if you point to the wrong file name.

Common Pitfalls

A common mistake is using wt when you meant to append. That silently wipes the file before writing.

Another mistake is assuming rt and wt are special Python-only magic. They are simply explicit combinations of read or write with text mode.

A third issue is omitting the encoding and then seeing different behavior across systems. Text mode is much safer when the encoding is specified deliberately.

Summary

  • 'rt means read text and wt means write text'
  • Text mode works with Python strings, not raw bytes
  • 'rt is effectively the same as plain r'
  • 'wt truncates the file before writing'
  • Specify encoding="utf-8" explicitly for predictable text handling

Course illustration
Course illustration

All Rights Reserved.