ValueError: Must have exactly one of create/read/write/append mode

avatar
Borislav Hadzhiev

Last updated: Feb 17, 2023
3 min

banner

# ValueError: Must have exactly one of create/read/write/append mode

The Python "ValueError: must have exactly one of create/read/write/append mode" occurs when we specify an incorrect mode when opening a file.

To solve the error, use the r+ mode if you need to open the file for both reading and writing.

valueerror must have exactly one of create read write append mode

Here is an example of how the error occurs.

main.py
# 👇️ using incorrect 'rw' mode # ⛔️ ValueError: must have exactly one of create/read/write/append mode with open('example.txt', 'rw', encoding='utf-8') as my_file: read_data = my_file.read() print(read_data) my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.seek(0) print(my_file.read())

We used an unsupported rw mode which caused the error.

# Opening a file for both reading and writing

If you need to open a file for both reading and writing, use the r+ mode instead.

main.py
# ✅ using r+ mode with open('example.txt', 'r+', encoding='utf-8') as my_file: read_data = my_file.read() print(read_data) my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.seek(0) print(my_file.read())

open file for reading and writing

The r+ mode opens the file for reading and writing and positions the stream at the beginning of the file.

If you run the code sample multiple times, you will write the lines multiple times to the file.

writing multiple times to the file

This is because when a file is opened using the r+ mode, its contents are not truncated.

If you want to truncate the file every time you open it, use the w+ mode instead.

# Using the w+ mode instead of r+

You might also see the w+ mode being used. It opens the file for reading and writing and creates the file if it doesn't already exist.

If the file exists, it is truncated. Truncating the file means that all contents of the file are erased.

main.py
with open('example-2.txt', 'w+', encoding='utf-8') as my_file: read_data = my_file.read() print(read_data) my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.seek(0) print(my_file.read())

using the w mode instead

On the other hand, the r+ mode doesn't create the file if it doesn't exist, nor does it truncate the file.

Both the r+ and w+ modes position the stream at the beginning of the file.

# Using the a+ mode to open the file for reading and writing

There is also an a+ mode which opens the file for reading and writing.

The a+ mode creates the file if it doesn't already exist, but it positions the stream at the end of the file.

main.py
with open('example-3.txt', 'a+', encoding='utf-8') as my_file: read_data = my_file.read() print(read_data) my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.seek(0) print(my_file.read())

The difference between the a+ mode and the other 2 is that the stream is positioned at the end of the file.

The file.seek(0) method can be used to position the stream at the beginning of the file.

The a+ mode also doesn't truncate the file if it already exists.

You can view all of the other possible modes to open a file in this table of the official docs.

Here are the most commonly used modes:

NameDescription
'r'open for reading
'w'open for writing (truncates the file first)
'a'open for writing, appending to the end of the file if it exists

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.