Python File Modes: Explained

Updated: August 27, 2023 By: Khue Post a comment

File modes are used to specify how you want to open a file, such as for reading, writing, appending, or binary data. Below is the complete list of file modes you can use in Python:

  • r: Opens a file for reading only. The file pointer is placed at the beginning of the file. If the file does not exist, it raises an error.
  • w: Opens a file for writing only. If the file does not exist, it creates a new file. If the file exists, it truncates (clears) the file before writing. The file pointer is placed at the beginning of the file.
  • a: Opens a file for appending data. The file pointer is placed at the end of the file. If the file does not exist, it creates a new file.
  • r+: Opens a file for both reading and writing. The file pointer is placed at the beginning of the file. If the file does not exist, it raises an error.
  • w+: Opens a file for both writing and reading. If the file does not exist, it creates a new file. If the file exists, it truncates (clears) the file before writing. The file pointer is placed at the beginning of the file.
  • a+: Opens a file for both appending and reading. The file pointer is placed at the end of the file. If the file does not exist, it creates a new file.
  • x: Opens a file for exclusive creation. It creates a new file and opens it for writing only. If the file already exists, it raises a FileExistsError exception. This mode is only available in Python 3.
  • b: Opens a file in binary mode for reading or writing binary data. This mode is typically used for non-text files like images or binary data. You can combine this mode with other modes, such as rb, wb, or ab.
  • t: Opens a file in text mode for reading or writing text data. This is the default mode if you do not specify any other mode. You can combine this mode with other modes, such as rt, wt, or at.
  • rb: Opens a file for reading only in binary mode. The file pointer is placed at the beginning of the file. If the file does not exist, it raises an error.
  • wb: Opens a file for writing only in binary mode. If the file does not exist, it creates a new file. If the file exists, it truncates (clears) the file before writing. The file pointer is placed at the beginning of the file.
  • ab: Opens a file for appending data in binary mode. The file pointer is placed at the end of the file. If the file does not exist, it creates a new file.
  • r+b or rb+: Opens a file for both reading and writing in binary mode. The file pointer is placed at the beginning of the file. If the file does not exist, it raises an error.
  • w+b or wb+: Opens a file for both writing and reading in binary mode. If the file does not exist, it creates a new file. If the file exists, it truncates (clears) the file before writing. The file pointer is placed at the beginning of the file.
  • a+b or ab+: Opens a file for both appending and reading in binary mode. The file pointer is placed at the end of the file. If the file does not exist, it creates a new file.

Understanding file modes in Python is important because they allow you to control how you interact with files, such as reading, writing, appending, or creating them. Depending on your needs, you can choose the appropriate file mode to avoid errors, preserve data integrity, or optimize performance.