[Fixed] “io.unsupportedoperation not readable” Error

Many times while dealing with file handling in Python, we get to face “io.unsupportedoperation not readable” error. It may cause us a heavy loss of time in debugging the issue. To make it easier for you, we will be discussing some ways which can cause this error and how to solve this issue. So, let’s move into the article without much loss of time.

To understand the error, we first need to understand its origins, and for that, we need to know something about file handling in Python.

What is IO in Python?

IO is a python module that helps the user handle files in Python. It provides functions using which we can perform different functions and manipulate them using Python. We can handle Text I/O, binary I/o, and raw I/O in Python by creating a file object. The file is also known by the name of a stream in Python.
We try to access the files and perform manipulations, and whenever any error occurs in the operations performed on the file, then IO exception is raised in Python.
We can open a file for various types of manipulation using the open() function in Python.

What is an open() function in Python?

open() is a built-in function in Python that is used to open the file which we want to access and create a file object.

The open() function looks like this:

open(Address_of_file , openingMode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Address_of_file is the required parameter which is the address of the file which we are trying to access.

What are various access modes?

An access mode is a way we want to open the file for manipulation.

Reading access modes

r: If we just want to read a file, then we can use this mode. By default, this is the mode if we don’t pass any access mode as its argument. The file pointer starts from the beginning of the file.

rb: It opens the file for reading in binary format. The file pointer reads from the beginning.

r+: It opens the file for both reading and writing.

rb+: Opens the file for both reading and writing in binary format. The pointer is placed from the beginning of the file.

Writing access modes

w: Opens the file for writing only. It creates a new file if the file does not exist or overrides the file if the file of the same name exists.

wb: This mode is similar to w, the only difference being it opens to file for writing only in binary format. If the file does not exist, then a new file is created; otherwise, access mode overwrites the file if the file of the same name exists.

w+: It will open a file for both reading and writing like ‘r+’. It also creates a new file if it does not exist already or overwrites the already existing file.

wb+: It opens a file for both readings and writing in the binary format like ‘rb+’. It also creates a new file if it does not exist already or overwrites the already existing file.

Append access modes

a: It appends the file which is being opened or creates a new file if it does not exist.

ab: It appends the file which is being opened in the binary format or creates a new file if it does not exist.

a+: It will open the file for both appending and reading.

ab+: It opens the file for appending and reading in binary format.

“io.unsupportedoperation not readable”

The “io.unsupportedoperation not readable” error means we are not able to read the file which we wish to. The “io.UnsupportedOperation: not readable” error typically occurs when you are trying to read from a file-like object that doesn’t support reading, such as a write-only file or a file that has been opened in write-only mode. Here’s an example of how this error can be caused:

import io
# Create a write-only file-like object
file = io.StringIO()
# Attempt to read from the file
file.read()

In this example, an io.StringIO object is a file-like object that is opened in write-only mode by default. When the read() method is called on this object, it raises the “io.UnsupportedOperation: not readable” error because the object doesn’t support reading.

Another example would be using open() to open file in write mode:

with open('file.txt', 'w') as file:
    contents = file.read()

Here, the file is opened in write mode using ‘w’ flag, so any read operation will result in this error message.

“io.unsupportedoperation not readable” while reading CSV

The “io.UnsupportedOperation: not readable” error occurs when you try to read from a file-like object that does not support the “read” operation. This can happen if you pass an object that is not a file or a file that is not open for reading to a function or method that expects a readable file.

When reading from a CSV file using the csv module, you may see this error if you have not opened the file for reading before passing it to the csv.reader() function.

Another reason this error might occur is that you are trying to read a file that does not exist. In that case, check the file path and directory you are trying to access, and also make sure that you have the correct permission to read that file.

It is also possible that the file you are trying to read is not a CSV file. Ensure that the file you are trying to read is in CSV format.

Also, ensure that the python version is compatible with the library you use to read the CSV files.

“io.unsupportedoperation not readable” while using json.load

The “io.UnsupportedOperation: not readable” error can occur when you’re trying to read JSON data using the json.load() function in Python, and the file-like object passed to the function does not support the “read” operation.

Here’s an example of how the error might be raised:

import json

# Create a file-like buffer to receive JSON data.
fake_json = io.StringIO("this is not a json file")

# Attempt to load the JSON data.
data = json.load(fake_json)

# Error will be raised here
# io.UnsupportedOperation: not readable

This program creates a fake JSON file-like object using the io.StringIO class, which is not actually a JSON file, and then attempts to read from it using the json.load() function. Because the fake_json object is not a readable file, the json.load() function raises the “io.UnsupportedOperation: not readable” error.

To avoid this error, make sure you are passing a file-like object that supports the “read” operation, such as a file object returned by the open() function in ‘r’ mode to the json.load() function.

with open('example.json', 'r') as json_file:
    data = json.load(json_file)
    # rest of the code here

Another reason this error might occur is that you are trying to read a file that does not exist. Check the file path and directory, and also make sure that you have the correct permission to read that file.

It is also possible that the file you are trying to read is not a JSON file. Make sure that the file you are trying to read is in JSON format.

Also, make sure that the python version is compatible with the library you are using to read the json files.

Error while using `self.fieldnames=next(self.reader)`

The “io.UnsupportedOperation: not readable” error can occur when the next(self.reader) statement is called and the self.reader object does not support the “read” operation.

The self.reader object is usually an instance of a csv.reader or json.load(), which requires a readable file-like object to be passed to the constructor.

If the self.reader is an instance of csv.reader the error could be due to the fact that the file passed to the csv.reader is not open for reading, or the file passed is not a CSV file.

Just follow the remedies written when we get the error in CSV or json.load, the error should get resolved.

`print(stdout.read().decode())` shows io.unsupportedoperation not readable error

The “io.UnsupportedOperation: not readable” error can occur when you’re trying to read the output of a subprocess using the stdout.read() method and the stdout object does not support the “read” operation.

This happens when you try to read the output of a process that has already been completed, and the output has been consumed or is no longer available.

You should make sure that you are only trying to read the output of a running or recently completed process.

For e.g.:-

import subprocess

# Start a process and capture its output.
process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)

# Wait for the process to complete
process.wait()

# Read the output from stdout and decode it from bytes to a string.
output = process.stdout.read().decode()

# Print the output.
print(output)

In this example, the subprocess.Popen() function is used to start a new process and capture its output. The process.wait() function is then used to wait for the process to complete; after that stdout.read() method is used to read the output and decode it.

It’s worth noting that, stdout attribute of a Popen object is a file-like object that supports reading on python 3 and higher, but on python 2, it is a regular string, so you might not need to use .decode() method in python 2.

Make sure that you are using the correct version of the subprocess library and Python.

It is also worth making sure that the process you’re trying to read from is actually running and producing output. Make sure that the command you’re running is valid and that you have the necessary permissions to run it.

Fixing “io.unsupportedoperation not readable” error

There are several ways to fix the “io.UnsupportedOperation: not readable” error in Python.

If you try to read from a file-like object and get this error, try opening the object in read-only mode. For example, if you’re using the io.StringIO class, you can create the object with the mode='r' parameter to open it in read-only mode:

import io
file = io.StringIO(mode='r')
contents = file.read()

You could also open files in r mode, which is read mode, instead of ‘w’ mode in the open() function.

with open('file.txt', 'r') as file:
    contents = file.read()

In general, find the file type object in the code causing the error and check for access mode permissions. If not, then try to open it in reading mode or use some other object.

This way, you can solve your error of ” io.unsupportedoperation not readable”.

References

  1. https://docs.python.org/3/library/functions.html#open

Frequently Asked Questions

Q.01. Which function is used to access any location inside the file?

Ans.01. We can use the .seek() function to position the pointer to any location.

Q.02. How to check if a file exists before reading it?

Ans.02. We can use os.path.exists() method to check whether the path exists or not.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments