14.3. File Modes

  • Default mode is read text mode (rt)

  • Text - easy to read and write

  • Binary - Fast and efficient

  • Read - Get data from file

  • Write - Save data to file (overwrite existing data)

  • Append - Add line to file

  • Update - Read and Write (rarely used)

By type:

  • Text - easy to read and write

  • Binary - Fast and efficient

By operation:

  • Read - Get data from file

  • Write - Save data to file (overwrite existing data)

  • Append - Add line to file

  • Update - Read and Write (rarely used)

If mode is not specified it will read in text mode (mode='r')

14.3.1. Example

  • If mode is not specified it will read in text mode (mode='r')

>>> open('/tmp/myfile.txt')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='r' encoding='UTF-8'>

14.3.2. Modes

  • r - For reading – The file pointer is placed at the beginning of the file. This is the default mode.

  • r+ - Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

  • w - Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • w+ - Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

  • rb - Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.

  • rb+ - Opens a file for both reading and writing in binary format.

  • wb+ - Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

  • a - Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • ab - Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • a+ - Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

  • ab+ - Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

  • x - Open for exclusive creation, failing if the file already exists (Python 3)

Source: [1]

14.3.3. Short Notation

  • Most commonly used

  • By default text mode is used

  • mode='r' - read in text mode (default)

  • mode='w' - write in text mode

  • mode='a' - append in text mode

>>> open('/tmp/myfile.txt', mode='r')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='r' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='w')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='w' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='a')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='a' encoding='UTF-8'>

14.3.4. Text Mode

  • Text - easy to read and write

  • mode='rt' - read in text mode

  • mode='wt' - write in text mode

  • mode='at' - append in text mode

>>> open('/tmp/myfile.txt', mode='rt')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='rt' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='wt')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='wt' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='at')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='at' encoding='UTF-8'>

14.3.5. Binary Mode

  • Binary - Fast and efficient

  • mode='rb' - read in binary mode

  • mode='wb' - write in binary mode

  • mode='ab' - append in binary mode

>>> open('/tmp/myfile.txt', mode='rb')
<_io.BufferedReader name='/tmp/myfile.txt'>
>>> open('/tmp/myfile.txt', mode='wb')
<_io.BufferedWriter name='/tmp/myfile.txt'>
>>> open('/tmp/myfile.txt', mode='ab')
<_io.BufferedWriter name='/tmp/myfile.txt'>

14.3.6. Update Mode

  • Reading and Writing

  • Text mode is used if not specified otherwise

  • mode='r+' - read in text mode

  • mode='w+' - write in text mode

  • mode='a+' - append in text mode

  • mode='rt+' - update in text mode

  • mode='wt+' - update in text mode

  • mode='at+' - update in text mode

  • mode='rb+' - update in binary mode

  • mode='wb+' - update in binary mode

  • mode='ab+' - update in binary mode

>>> open('/tmp/myfile.txt', mode='r+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='r+' encoding='UTF-8'>
>>>
>>> open('/tmp/myfile.txt', mode='w+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='w+' encoding='UTF-8'>
>>>
>>> open('/tmp/myfile.txt', mode='a+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='a+' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='rt+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='rt+' encoding='UTF-8'>
>>>
>>> open('/tmp/myfile.txt', mode='wt+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='wt+' encoding='UTF-8'>
>>>
>>> open('/tmp/myfile.txt', mode='at+')
<_io.TextIOWrapper name='/tmp/myfile.txt' mode='at+' encoding='UTF-8'>
>>> open('/tmp/myfile.txt', mode='rb+')
<_io.BufferedRandom name='/tmp/myfile.txt'>
>>>
>>> open('/tmp/myfile.txt', mode='wb+')
<_io.BufferedRandom name='/tmp/myfile.txt'>
>>>
>>> open('/tmp/myfile.txt', mode='ab+')
<_io.BufferedRandom name='/tmp/myfile.txt'>

14.3.7. Recap

Most common (90% of cases):

  • mode='r' - read in text mode (default)

  • mode='w' - write in text mode

  • mode='a' - append in text mode

Text Mode:

  • mode='rt' - read in text mode

  • mode='wt' - write in text mode

  • mode='at' - append in text mode

Binary Mode:

  • mode='rb' - read in binary mode

  • mode='wb' - write in binary mode

  • mode='ab' - append in binary mode

Update (rarely used):

  • mode='rb+' - update in binary mode

  • mode='wb+' - update in binary mode

  • mode='ab+' - update in binary mode

  • mode='r+' - read in text mode

  • mode='w+' - write in text mode

  • mode='a+' - append in text mode

  • If mode is not specified it will read in text mode (mode='rt')

14.3.8. References

14.3.9. Assignments

# %% About
# - Name: File Mode Write
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with `FILE` opened in write mode (text)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `FILE` otwartym w trybie zapisu (text)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `open()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from os import remove
>>> remove(FILE)

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is TextIOWrapper, \
'Variable `result` has invalid type, should be TextIOWrapper'

>>> assert result.mode in ('w', 'wt'), \
'File `result` must be opened in write mode (text)'

>>> assert result.encoding == 'UTF-8', \
'File `result` must be opened with UTF-8 encoding'

>>> assert result.name == FILE, \
'File `result` must be opened with name `FILE`'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
from io import TextIOWrapper

# %% Types
result: TextIOWrapper

# %% Data
FILE = '_temporary.txt'

# %% Result
result = ...

# %% About
# - Name: File Mode Append
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with `FILE` opened in append mode (text)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `FILE` otwartym w trybie dopisywania (text)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `open()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from os import remove
>>> remove(FILE)

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is TextIOWrapper, \
'Variable `result` has invalid type, should be TextIOWrapper'

>>> assert result.mode in ('a', 'at'), \
'File `result` must be opened in append mode (text)'

>>> assert result.encoding == 'UTF-8', \
'File `result` must be opened with UTF-8 encoding'

>>> assert result.name == FILE, \
'File `result` must be opened with name `FILE`'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
from io import TextIOWrapper

# %% Types
result: TextIOWrapper

# %% Data
FILE = '_temporary.txt'

# %% Result
result = ...

# %% About
# - Name: File Mode Read
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with `FILE` opened in read mode (text)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `FILE` otwartym w trybie odczytu (text)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `open()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from os import remove
>>> remove(FILE)

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is TextIOWrapper, \
'Variable `result` has invalid type, should be TextIOWrapper'

>>> assert result.mode in ('r', 'rt'), \
'File `result` must be opened in read mode (text)'

>>> assert result.encoding == 'UTF-8', \
'File `result` must be opened with UTF-8 encoding'

>>> assert result.name == FILE, \
'File `result` must be opened with name `FILE`'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
from io import TextIOWrapper

# %% Types
result: TextIOWrapper

# %% Data
FILE = '_temporary.txt'
open(FILE, mode='w')

# %% Result
result = ...

# %% About
# - Name: File Mode Read
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define `result` with `FILE` opened in read mode and 'cp1250' encoding
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result` z `FILE` otwartym w trybie odczytu i kodowaniu 'cp1250'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `open()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from os import remove
>>> remove(FILE)

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is TextIOWrapper, \
'Variable `result` has invalid type, should be TextIOWrapper'

>>> assert result.mode in ('r', 'rt'), \
'File `result` must be opened in read mode (text)'

>>> assert result.encoding == 'cp1250', \
'File `result` must be opened with cp1250 encoding'

>>> assert result.name == FILE, \
'File `result` must be opened with name `FILE`'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
from io import TextIOWrapper

# %% Types
result: TextIOWrapper

# %% Data
FILE = '_temporary.txt'
open(FILE, mode='w')

# %% Result
result = ...

# %% About
# - Name: File Mode Read
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with `FILE` opened in read mode (binary)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `FILE` otwartym w trybie odczytu (binary)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `open()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from os import remove
>>> remove(FILE)

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is BufferedReader, \
'Variable `result` has invalid type, should be BufferedWriter'

>>> assert result.mode == 'rb', \
'File `result` must be opened in read mode (binary)'

>>> assert result.name == FILE, \
'File `result` must be opened with name `FILE`'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
from io import BufferedReader

# %% Types
result: BufferedReader

# %% Data
FILE = '_temporary.txt'
open(FILE, mode='wb')

# %% Result
result = ...