File handling is one of the essential concepts in any programming language but the exception “io.unsupportedOperation: not readable” can be a common real problem when you are dealing with files on your local machine.
In python, file handling is not that complicated, and the exception “io. unsupported operation: not readable” can handle easily. When you open a file to read data from it and, mistakenly, you open it in write mode, it will cause an exception.
In this article, we’ll discuss the io.unsupportedOperation in detail, like why this error occurs and how we can fix it. Before diving into the article, we must know about these two most important concepts: file handling io module and how to open a file for any purpose in Python.
Table of Contents
What is The IO Module in Python?
The Python io module provides the core facilities for file handling. Basically, Python handles three main types of I/O: Text I/O, binary I/O, and raw I/O. with the help of a concrete object of a file object.
The file is also known as a stream. We can easily access and manipulate accessible files with the help of file objects and whenever a file operation fails, it raises the IO exception in python.
To deal with files first it’s necessary to open the file and Python has facilitated us with the open() function that helps us open a file to interact with the data on the local machine.
How to Open a File in Python?
We can easily open a file in python with the help of a built-in function open(). This method opens a file and returns the corresponding file object.
The general syntax of the open method is:
open(fileAddress , openingMode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
In this syntax, we have two required parameters file address and opening mode. File address means the exact path of the file from where we want to open a file or where we want to create a file if it does not exist. File opening mode is the access mode in which we want to open a file.
The following are the different access modes we use:
Access Mode | Description |
r | It opens a file for reading only. In this mode file pointer is placed at the beginning of the file; if we don’t write any access mode, it is our default mode. |
rb | The rb stands for reading only in binary. It opens a file for reading only in binary format. The file pointer reads from the beginning of the file. |
r+ | If we write + after r, it opens a file for reading and writing. In this mode, the file pointer is placed at the beginning of the file. |
rb+ | If we write + after rb, it opens a file for both readings and writing in binary format. In this mode, the file pointer is placed at the beginning of the file. |
w | If we want to open a file for writing, we have to write w to open a file for writing only. In this mode, a new file is created if the file does not exist. This access mode overwrites the file if the file exists. |
wb | Just like rb, we write wb to open a file for writing only in binary format. In this mode, a new file is created if the file does not exist on a given path. This access mode overwrites the file if the file exists. |
w+ | If we write w+, it will open a file for writing and reading. This mode also overwrites the existing file if the file exists, like w and wb. If the file does not exist on the given path, create a new file for reading and writing. |
wb+ | Like rb+, writing wb+ mode will open a file for writing and reading in binary format. In this mode, a new file is created if the file does not exist. This access mode overwrites the file if the file exists. |
a | a stand for appending means writing something at the end of the file. If a file does not exist, it creates a new one. |
ab | If we write ab as access mode, it will open a file for appending in binary format. |
a+ | We write a+ for opening a file for both appending and reading. |
ab+ | We write ab+ for opening a file for both appending and reading in binary format. |
Now, we are in a position to understand io.unsupportedoperation: not readable error.
What Are The Reasons of IO.UnsupportedOperation: Not Readable Error?
Whenever you want to open a file for reading purposes, we have to write r as an access mode, and if we by mistake write w in place of r, it will cause an error. Now let’s understand it with the help of an example:
Code
# open a file to read it fileobj = open("myfile1.txt", 'w') # read file and store it in text text = fileobj.read() # print value of text on screen print(text) fileobj.close()
Output
In this example, we wrote w as access mode, which causes an error io.unsupportedoperation: not readable. Now, move straight forward to its solution.
How to Fix IO.UnsupportedOperation: Not Readable Error in Python?
This exception is caused by entering the wrong access modes; if we are opening a file for just a reading purpose, then we have to use r, and if we want to open it for two purposes, like reading and writing or reading and append, then we have to write appropriate mode. For reading and writing, we can use r+; for reading and append, we can use a+. Now let’s explain it with the help of an example:
Code
# to open a file for just read purpose write mode r # to open a file for read and write purpose we write r+ fileobj = open("myfile1.txt", 'r+') # read file and store it in text text = fileobj.read() # print value of text on screen print(text) fileobj.close()
Output
>>>This is the text of my file.
In this example, our file myfile1.txt already exists with the text “This is the text of my file.” that’s why we get this output; otherwise, we get an error FileNotFoundError if you doubt full whether a file is created, then we can also use append mode. Now let’s understand this concept with the help of an example:
Code
# to open a file for read and append purposes we write a+ fileobj = open("myfile1.txt", 'a+') # read file and store it in text text = fileobj.read() # print value of text on screen print(text) fileobj.close()
Output
>>>
Yes, this code will show nothing because our file pointer is at the end of the file; due to append mode we have to write fileobj.seek(0) to move the file pointer at the start of the file for reading or we can first append then read.
Conclusion
To summarize the article, we have discussed how to fix io.unsupportedoperation: not readable error in python. We have explained its solution with the help of two simple examples. Furthermore, we’ve discussed the io module and open method in detail.
Let’s have a quick summation of the topics debated in this writing.
- What is io module in python?
- What is the function of the open method?
- What is the reason for io.unsupportedoperation: not readable error in python?
- How to fix io.unsupportedoperation: not readable error?
Finally, you broadly understand the “io.unsupportedoperation: not readable” error in python; it’s time to recall the concepts; comment below 👇 an access mode suitable in your case.