Python - Understanding File Modes r+, w+ and a+ in open()

In this tutorial, we’ll learn the differences between r, r+, w, w+, a, and a+ in Python’s open() function. These modes allow you to read, write, append or do combination of these.

Essentially, the mode determines how the file is opened and how you can interact with its contents. For example, if you want to read data from a file, you need to open the file in “read” mode. If you want to write data to a file, you need to open the file in “write” mode. If you want to append data to an existing file, you need to open the file in “append” mode. Choosing the wrong file mode can cause errors or unexpected behavior in your program. For example, if you try to write to a file that is opened in “read” mode, you will get an error.

Here’s a quick comparison of file open modes r, r+, w, w+, a, and a+:

Mode Description File Pointer Position Creates File if Not Exists Truncates Existing File
r Read-only Beginning of the file No No
r+ Read and write (updating) Beginning of the file No No
w Write-only (overwrite or create) Beginning of the file Yes Yes
w+ Write and read (overwrite or create) Beginning of the file Yes Yes
a Append-only (append or create) End of the file Yes No
a+ Append and read (append or create) End of the file Yes No

r

The r mode opens the file for reading only. The file pointer is placed at the beginning of the file. If the file does not exist, open() raises a FileNotFoundError.

with open("file.txt", "r") as f:
    content = f.read()

r+

The r+ mode opens the file for both reading and writing. Important to note that like r, If the file does not exist, open() raises a FileNotFoundError.

with open("file.txt", "r+") as f:
    content = f.read()
    f.write("New content")

w

The w mode opens the file for only writing. You can NOT read in this mode. The file pointer is placed at the start of the file. If the file exists, its content is truncated. If the file does not exist, it is created. Be careful to avoid any data loss.

with open("file.txt", "w") as f:
    f.write("New content")

w+

The w+ mode opens the file for both writing and reading. Like w, it too will truncate if the the file exists, or create a new one if it doesn’t.

with open("file.txt", "w+") as f:
    f.write("New content")
    f.seek(0)
    content = f.read()

a

The a mode opens the file for appending. The file pointer is placed at the end of the file, so new content is added after the existing content. If the file does not exist, it is created.

Note: The key difference between a and w is that w truncates the existing content of the file, while a appends data to the end of the file without modifying the existing content.

with open("file.txt", "a") as f:
    f.write("Appended content")

a+

The a+ mode opens the file for both appending and reading. The file pointer is placed at the end of the file, so new content is added after the existing content. If the file does not exist, it is created.

with open("file.txt", "a+") as f:
    f.write("Appended content")
    f.seek(0)
    content = f.read()

Hope this helped you understand different file modes in Pythons and how to choose the appropriate mode.



Speak Your Mind

-->