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:
- '
rmeans open for reading' - '
wmeans open for writing' - '
tmeans treat the file as text' - '
bmeans treat the file as binary'
So these are equivalent:
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.
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.
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:
and:
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.
That difference is critical:
- '
wtoverwrites from the start after truncating' - '
atappends to the end'
Text Mode Versus Binary Mode
Text mode returns strings and performs encoding and newline handling. Binary mode returns raw bytes.
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.
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
- '
rtmeans read text andwtmeans write text' - Text mode works with Python strings, not raw bytes
- '
rtis effectively the same as plainr' - '
wttruncates the file before writing' - Specify
encoding="utf-8"explicitly for predictable text handling

