Table of Contents » Reference Materials : File Access Modes
File Access Modes
Python file access modes are used when opening a file with the built-in open() function, determining how the file will be accessed and manipulated. The primary modes include 'r' for reading (default mode), 'w' for writing (overwrites existing file or creates a new one), 'a' for appending (writes data to the end of the file if it exists), and 'x' for exclusive creation (fails if the file already exists). In addition to these, you can add a 'b' to open the file in binary mode (e.g., 'rb', 'wb') for non-text files like images or videos, and a '+' for updating (reading and writing simultaneously, e.g., 'r+', 'w+'). Each mode serves a specific purpose, allowing controlled interaction with files, ranging from simple text reading to complex binary data manipulation. Understanding and selecting the appropriate access mode is crucial for effective file handling in Python.
File Access Mode | Description |
---|---|
|
Opens the specified file for read only access. Upon opening the file pointer is positioned at the very beginning of the file. If file_access_mode is left out of the open() statement, this is the default access mode. |
|
Opens the specified file for binary read only access. Upon opening, the file pointer is positioned at the very beginning of the file. |
|
Opens the specified file for both read and write access. Upon opening, the file pointer is positioned at the very beginning of the file. |
|
Opens the specified binary file for both read and write access. Upon opening, the file pointer is positioned at the very beginning of the file. |
|
Opens the specified file for writing only. If the file exists already at the location specified, the existing file is overwritten. If the file does not exist already, the file is created with the name specified. |
|
Opens the specified binary file for writing only. If the file exists already at the location specified, the existing file is overwritten. If the file does not exist already, the file is created with the name specified. |
|
Opens the specified file for both reading and writing. If the file exists already at the location specified, the existing file is overwritten. If the file does not exist already, the file is created with the name specified. |
|
Opens the specified binary file for both reading and writing. If the file exists already at the location specified, the existing file is overwritten. If the file does not exist already, the file is created with the name specified. |
|
Opens the specified file for appending. If the file exists, the file pointer is positioned at the end of the file and the file is set to append mode, that is, any new data written to the file will be appended (added) to the end of the file. If the specified file does not already exist, then the file is created using the file name specified, the file pointer will be at the beginning of the empty file and the file will be set for writing. |
|
Opens the specified binary file for appending. If the file exists, the file pointer is positioned at the end of the file and the file is set to append mode, that is, any new data written to the file will be appended (added) to the end of the file. If the specified file does not already exist, then the file is created using the file name specified, the file pointer will be at the beginning of the empty file and the file will be set for writing. |
|
Opens the specified file for appending and reading. If the file exists, the file pointer is positioned at the end of the file and the file is set to append mode, that is, any new data written to the file will be appended (added) to the end of the file. If the specified file does not already exist, then the file is created using the file name specified, the file pointer will be at the beginning of the empty file and the file will be set for writing. |
|
Opens the specified binary file for appending and reading. If the file exists, the file pointer is positioned at the end of the file and the file is set to append mode, that is, any new data written to the file will be appended (added) to the end of the file. If the specified file does not already exist, then the file is created using the file name specified, the file pointer will be at the beginning of the empty file and the file will be set for writing. |