io.UnsupportedOperation: not readable/writable Python Error

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
7 min

banner

# Table of Contents

  1. io.UnsupportedOperation: not readable
  2. io.UnsupportedOperation: not writable

If you got the error "io.UnsupportedOperation: not writable", click on the second subheading.

# io.UnsupportedOperation: not readable

The Python error "io.UnsupportedOperation: not readable" occurs when you open a file for writing and try to read from the file.

To solve the error, open the file for reading with the r mode or use the a+ mode to open the file for reading and writing.

io unsupported operation not readable

Here is an example of how the error occurs.

main.py
with open('example.txt', 'w', encoding='utf-8') as f: # ⛔️ io.UnsupportedOperation: not readable lines = f.readlines() print(lines)

The second argument we passed to the open() function is the mode.

The w mode is used to open the file for writing (it also truncates the file).

Suppose you have a file called example.txt in the same directory as your main.py file.

example.txt
bobby hadz com

Use the r mode to open the file for reading to solve the error.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) # 👉️ ['bobby\n', 'hadz\n', 'com']

open file for reading instead

The code for this article is available on GitHub

The r mode is used for reading, so everything works as expected.

You can also use the read() method if you want to read the file's contents into a string.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: text = f.read() # bobby # hadz # com print(text)

If you got the error "io.UnsupportedOperation: not writable", then you are trying to write to a file that is opened using the r (reading) mode and should use the w (writing) mode instead.

# Opening the file for both reading and writing

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

The a+ mode opens the file for reading and writing and creates the file if it doesn't exist.
main.py
with open('example.txt', 'a+', encoding='utf-8') as f: read_data = f.read() print(read_data) f.write('\nfirst line' + '\n') f.write('second line' + '\n') f.write('third line' + '\n') f.seek(0) print(f.read())
The code for this article is available on GitHub

Assuming you have an example.txt file with the following contents before running the main.py script.

example.txt
bobby hadz com

Running the python main.py command updates the example.txt file as follows.

example.txt
bobby hadz com first line second line third line

open file for reading and writing

The a+ mode also takes care of creating the file if it doesn't already exist.

The file.seek(0) call can be used to move the position of the file reader to index 0 to be able to read the file's contents from the beginning.

main.py
with open('example.txt', 'a+', encoding='utf-8') as f: # ... f.seek(0) print(f.read())

In other words, the f.seek(0) call moves you to the beginning of the file.

Here is a table of the supported modes and their meaning.

ModeDescription
rOpens a file for reading only (default).
r+Opens a file for reading and writing.
rbOpens a file for reading only in binary format.
rb+Opens a file for reading and writing in binary format.
wOpens a file for writing only (the file is truncated first).
aOpens a file for writing, appending to the end of the file if it exists.
a+Opens a file for reading and writing. The file is created if it doesn't exist

If you want to open a file for reading and writing but don't want to create the file if it doesn't already exist, use the r+ mode.

The + at the end of a flag means "open for updating (reading and writing)".

You can check the table in this section of the docs.

# Using the open() function instead of the with open() statement

The examples use the with open() statement.

The with open() statement should be your preferred approach because it automatically takes care of closing the file even if an error occurs.

When using the open() function, you have to manually close the file.

Here is an example of using the open() function for reading a file.

main.py
a_file = open('example.txt', 'r', encoding='utf-8') lines = a_file.readlines() print(lines) # 👉️ ['bobby\n', 'hadz\n', 'com'] a_file.close()
The code for this article is available on GitHub

Notice that we explicitly had to call the file.close() method after we're done.

Make sure to close your files to avoid memory leaks.

You can also use the open() function for reading and writing with the a+ mode.

main.py
a_file = open('example.txt', 'a+', encoding='utf-8') read_data = a_file.read() print(read_data) a_file.write('\nfirst line' + '\n') a_file.write('second line' + '\n') a_file.write('third line' + '\n') a_file.seek(0) print(a_file.read()) a_file.close()

open function reading and writing

Make sure to close the file after you're done.

# io.UnsupportedOperation: not writable

The Python error "io.UnsupportedOperation: not writable" occurs when you open a file in reading mode and try to write to the file.

To solve the error, open the file in writing mode (w) or open the file in reading and writing mode (a+) if you need to do both.

io unsupported operation not writable

Here is an example of how the error occurs.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: # ⛔️ io.UnsupportedOperation: not readable f.write('bobby' + '\n') f.write('hadz' + '\n') f.write('com' + '\n')

We opened the file in reading mode (r) and tried to write to the file.

This is not allowed because the r mode opens the file for reading only.

To solve the error, use the w (writing) mode when opening the file.

main.py
with open('example.txt', 'w', encoding='utf-8') as f: f.write('bobby' + '\n') f.write('hadz' + '\n') f.write('com' + '\n')
The code for this article is available on GitHub

Running the code sample with python main.py creates an example.txt file in the same directory.

example.txt
bobby hadz com

The w (writing) mode opens the file for writing. Note that the file is truncated first.

# Appending text to the contents of the file

If you want to append lines to the file, use the a (append) mode when opening the file.

main.py
with open('example.txt', 'a', encoding='utf-8') as f: f.write('first' + '\n') f.write('second' + '\n') f.write('third' + '\n')
The code for this article is available on GitHub

Suppose you have the following example.txt file before running the code sample.

example.txt
bobby hadz com

Here is the updated example.txt file after running python main.py.

example.txt
bobby hadz com first second third

The a flag opens the file for writing, append to the end of the file if it exists.

# Use the a+ mode if you need to read the file and write to it

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

The a+ mode opens the file for reading and writing and creates the file if it doesn't exist.
main.py
with open('example.txt', 'a+', encoding='utf-8') as f: read_data = f.read() print(read_data) f.write('\nfirst line' + '\n') f.write('second line' + '\n') f.write('third line' + '\n') f.seek(0) print(f.read())
The code for this article is available on GitHub

Assuming you have an example.txt file with the following contents before running the main.py script.

example.txt
bobby hadz com

Running the python main.py command updates the example.txt file as follows.

example.txt
bobby hadz com first line second line third line

open file for reading and writing

The a+ mode also takes care of creating the file if it doesn't already exist.

The file.seek(0) call can be used to move the position of the file reader to index 0 to be able to read the file's contents from the beginning.

main.py
with open('example.txt', 'a+', encoding='utf-8') as f: # ... f.seek(0) print(f.read())

In other words, the f.seek(0) call moves you to the beginning of the file.

Here is a table of the supported modes and their meaning.

ModeDescription
rOpens a file for reading only (default).
r+Opens a file for reading and writing.
rbOpens a file for reading only in binary format.
rb+Opens a file for reading and writing in binary format.
wOpens a file for writing only (the file is truncated first).
aOpens a file for writing, appending to the end of the file if it exists.
a+Opens a file for reading and writing. The file is created if it doesn't exist

If you want to open a file for reading and writing but don't want to create the file if it doesn't already exist, use the r+ mode.

The + at the end of a flag means "open for updating (reading and writing)".

You can check the table in this section of the docs.

# Using the open() function instead of the with open() statement

The examples use the with open() statement.

The with open() statement should be your preferred approach because it automatically takes care of closing the file even if an error occurs.

When using the open() function, you have to manually close the file.

Here is an example of using the open() function for writing to a file.

main.py
a_file = open('example.txt', 'w', encoding='utf-8') a_file.write('first' + '\n') a_file.write('second' + '\n') a_file.write('third' + '\n') a_file.close()
The code for this article is available on GitHub

We opened the file and stored the file object in a variable.

Note that after writing to the file, we call to file.close() method to close it.

This is done for you automatically when you use the with open() statement.

When using the open() function, you have to manually close the file to avoid memory leaks.

You can also use the open() function for reading and writing with the a+ mode.

main.py
a_file = open('example.txt', 'a+', encoding='utf-8') read_data = a_file.read() print(read_data) a_file.write('\nfirst line' + '\n') a_file.write('second line' + '\n') a_file.write('third line' + '\n') a_file.seek(0) print(a_file.read()) a_file.close()

open function reading and writing

The code for this article is available on GitHub

Make sure to close the file after you're done.

# 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.