Last updated: Apr 11, 2024
Reading time·7 min
If you got the error "io.UnsupportedOperation: not
writable
", click on the second subheading.
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.
Here is an example of how the error occurs.
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.
bobby hadz com
Use the r
mode to open the file for reading to solve the error.
with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) # 👉️ ['bobby\n', 'hadz\n', 'com']
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.
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.
If you need to open the file for both reading and writing, use the a+
mode.
a+
mode opens the file for reading and writing and creates the file if it doesn't exist.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())
Assuming you have an example.txt
file with the following contents before
running the main.py
script.
bobby hadz com
Running the python main.py
command updates the example.txt
file as follows.
bobby hadz com first line second line third line
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.
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.
Mode | Description |
---|---|
r | Opens a file for reading only (default). |
r+ | Opens a file for reading and writing. |
rb | Opens a file for reading only in binary format. |
rb+ | Opens a file for reading and writing in binary format. |
w | Opens a file for writing only (the file is truncated first). |
a | Opens 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.
with open()
statementThe 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.
a_file = open('example.txt', 'r', encoding='utf-8') lines = a_file.readlines() print(lines) # 👉️ ['bobby\n', 'hadz\n', 'com'] a_file.close()
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.
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()
Make sure to close the file after you're done.
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.
Here is an example of how the error occurs.
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.
with open('example.txt', 'w', encoding='utf-8') as f: f.write('bobby' + '\n') f.write('hadz' + '\n') f.write('com' + '\n')
Running the code sample with python main.py
creates an example.txt
file in
the same directory.
bobby hadz com
The w
(writing) mode opens the file for writing. Note that the file is
truncated first.
If you want to append lines to the file, use the a
(append) mode when opening
the file.
with open('example.txt', 'a', encoding='utf-8') as f: f.write('first' + '\n') f.write('second' + '\n') f.write('third' + '\n')
Suppose you have the following example.txt
file before running the code
sample.
bobby hadz com
Here is the updated example.txt
file after running python main.py
.
bobby hadz com first second third
The a
flag opens the file for writing, append to the end of the file if it
exists.
a+
mode if you need to read the file and write to itIf you need to open the file for both reading and writing, use the a+
mode.
a+
mode opens the file for reading and writing and creates the file if it doesn't exist.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())
Assuming you have an example.txt
file with the following contents before
running the main.py
script.
bobby hadz com
Running the python main.py
command updates the example.txt
file as follows.
bobby hadz com first line second line third line
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.
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.
Mode | Description |
---|---|
r | Opens a file for reading only (default). |
r+ | Opens a file for reading and writing. |
rb | Opens a file for reading only in binary format. |
rb+ | Opens a file for reading and writing in binary format. |
w | Opens a file for writing only (the file is truncated first). |
a | Opens 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.
open()
function instead of the with open()
statementThe examples use the with open() statement.
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.
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()
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.
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()
Make sure to close the file after you're done.
You can learn more about the related topics by checking out the following tutorials: