In this table the different access modes are explained in more detail. It can help to chose the right access mode when you try to open a file in Python.
access mode |
description |
r |
opens the file in reading mode only (default mode). The pointer is at the beginning of the file. |
rb |
opens the file for reading only in binary format. The pointer is at the beginning of the file. |
r+ |
opens the file for reading and writing. The pointer is at the beginning of the file. |
rb+ |
opens the file for reading and writing in binary format. The pointer is at the beginning of the file. |
w |
opens the file for writing only and overwrites the file if it already exists. If the file doesn't exists, a new file is created. |
wb |
opens the file for writing only in binary format and overwrites the file if it already exists. If the file doesn't exists, a new file is created. |
w+ |
opens a file for writing and reading. If the file exists, it will overwrite the content. If it doesn't exists, a new file is created. |
wb+ |
opens a file for writing and reading in binary format. If the file exists, it will overwrite the content. If it doesn't exists, a new file is created. |
a |
opens a file in append mode. this means the file pointer is at the end of the file and additional content is appended. If the file doesn't exists, a new file is created. |
ab |
opens a file in append mode in binary format. this means the file pointer is at the end of the file and additional content is appended. If the file doesn't exists, a new file is created. |
a+ |
opens a file in append and reading mode. this means the file pointer is at the end of the file and additional content is appended. If the file doesn't exists, a new file is created. |
ab+ |
opens a file in append and reading mode in binary format. this means the file pointer is at the end of the file and additional content is appended. If the file doesn't exists, a new file is created. |