89DEVs

Python File Modes

In Python files are opened using the built-in open() function. The second argument of the function call is used to specify the access mode in which the file is accessed. The access mode parameter is optional and by default the access mode is read only (r). 

If a file is opened in mode 'w' the content of the file is overwritten and the old content is replaced by new content. To append to an existing file, the file access mode 'a' (for append) is used. To open a file in binary mode the letter b is added to the first letter: rb for read only in binary mode, wb for overwrite in binary mode and ab for append in binary mode.

Overview of Python File Modes

The table gives an quick overview of the the file modes in Python. If a table field is marked with a plus sign it means the operation is used in the specified file mode.
operation / file mode r r+ w w+ a a+
read + + + +
write + + + + +
create (if file doesn't exist) + + + +
truncate + +
file pointer at start + + + +
file pointer at end + +

Access Modes & Description

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.

                
        

Summary


Click to jump to section