Skip to content

Python file modes | Open, Write, append (r, r+, w, w+, x, etc)

When you do work with the file in Python you have to use modes for specific operations like create, read, write, append, etc. This is called Python file modes in file handling.

Python file modes Open, Write, append

Python file modes

Don’t confuse, read about every mode below.

  • 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, create 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)
ModeDescription
“r”Read mode. Opens a file for reading. Raises an error if the file does not exist.
“w”Write mode. Opens a file for writing. If the file exists, it truncates (clears) the file before writing. If the file doesn’t exist, it creates a new file.
“x”Exclusive creation mode. Opens a file for exclusive creation. If the file already exists, it raises a FileExistsError exception.
“a”Append mode. Opens a file for appending data. The file pointer is at the end of the file, so the contents are added to the end. If the file doesn’t exist, it creates a new file.
“b”Binary mode. 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.
“t”Text mode. Opens a file in text mode, which is the default mode. This mode is used for reading or writing text data.
“+”Read and write mode. Adds the ability to read and write to a file. For example, "r+" opens a file for both reading and writing.

You can combine the modes as needed by specifying them in a string. For example, "rb" represents opening a file in binary mode for reading.

Let’s See a basic Example of the use of File mode

Simple example code.

Create File

f = open("cFile.txt", "w")

More Examples of creating a file: Python Create File (Empty Text File)

Write File

Now writing the file. w – Write mode

f = open("testFile.txt", "w")
f.write("Hello file")

Here are a few examples of opening files with different modes:

# Read mode
file = open("example.txt", "r")

# Write mode (creates a new file if it doesn't exist)
file = open("example.txt", "w")

# Append mode (creates a new file if it doesn't exist)
file = open("example.txt", "a")

# Binary mode for reading
file = open("example.bin", "rb")

# Read and write mode
file = open("example.txt", "r+")

Do comment if you have any doubts or suggestions on this tutorial.

Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

2 thoughts on “Python file modes | Open, Write, append (r, r+, w, w+, x, etc)”

  1. In the Quiz:
    > file = open(“c:\\ textFile.txt”, “r”)
    has invalid quote characters, an erroneous space before the file name, and teaches rather bad form for not using appropriate file names.

Leave a Reply

Your email address will not be published. Required fields are marked *