Io UnupportedOperation Not Readable

Io UnupportedOperation Not Readable

  1. Different Modes of File Handling in Python
  2. Open a File in Python
  3. Cause of the UnsupportedOperation: not readable Error in Python
  4. Solve the UnsupportedOperation: not readable Error in Python

File handling is one of the core concepts of computer programming that allow us to interact with a file on our local machines, manipulate them accordingly, and update them. To access files from your local machines, we use certain modes for different purposes to achieve the required goal efficiently.

Different Modes of File Handling in Python

As discussed in Python, you can utilize a few different modes to achieve the desired goal of interacting with files on your local computer. These modes depend on how you interact with files.

If you only want to read some data, you would use the read mode. On the other hand, if you wanted to write or append some data into your file, you would use the write mode because your desired goal is to append data into the file.

Syntax of opening a file in Python:

# opening a file
my_file = open("file path", "Mode")

Below are the different modes of file handing in Python.

  1. r is the read-only mode of a file
  2. r+ is used to open a file for reading and writing but can’t truncate the file.
  3. w is used to open a file in the write-only mode so that you can append data into the file
  4. w+ is also used for both reading and writing, but the difference is that you can truncate the file
  5. rb stands for read binary, which means only reading the file in binary format, and the pointer is placed at the start of the file.
  6. rb+ allows you to read and write the file in binary format.
  7. wb+ stands for write binary, which means write or append data into the file in binary format

Open a File in Python

To open a file from your local computer in Python script, you need to use the open keyword and provide arguments for the file’s path and mode according to your desired goal.

Suppose you want to access the file name demoFile.txt in read mode.

# open a file in read-only mode
read_file = open("demoFile.txt", "r")

# print the data from the file
print(read_file.read())

Output:

Hey Reader!
The file is in read-only mode

Cause of the UnsupportedOperation: not readable Error in Python

The UnsupportedOperation: not readable is one of the most common errors when you are new to file handling in Python, which says the operation is not supported and you cannot read the file.

The error UnsupportedOperation: not readable is that you have opened a file in write mode and are trying to read it, which is against the rules of Python.

Let’s see an example and understand why this error occurs.

# open a file (demoFile.txt)
demo_file = open("demoFile.txt","w")

# write data into the (demoFile.txt) file
demo_file.write("Continue your learning at DelfStack!")

# read the file
print(demo_file.read())

# close the file
demo_file.close()

Output:

----> 5 print(demo_file.read())
UnsupportedOperation: not readable

As you can see in the above code, we have opened the file in w mode to write only in the file, and parallelly we are trying to read data from the file in write-only mode.

Solve the UnsupportedOperation: not readable Error in Python

To solve the UnsupportedOperation: not readable error, you will need to close the file after writing data into it and reopen it in read mode, or you can use both read and write mode to perform both operations.

To understand it better, let’s see an example.

# open a file (demoFile.txt)
demo_file = open("demoFile.txt","w")

# write data into the (demoFile.txt) file
demo_file.write("Continue your learning at DelfStack!")

# close the file
demo_file.close()

# reopening the (demoFile.txt) file in read mode
read_demo_file = open("demoFile.txt", "r")

# read the (demoFile.txt) file
print(read_demo_file.read())

# close the file
demo_file.close()

Output:

Continue your learning at DelfStack!

You use the w+ or r+ when your concerns are reading and writing data on a file. The only difference between w+ and r+ is the r+ looks for the file.

If it exists, it opens the file; otherwise, it gives an error, whereas w+ creates an empty file if it doesn’t find it.

We will use r+ mode in this example because the file is already created.

# open a file (demoFile.txt)
demo_file = open("demoFile.txt","r+")

# write data into the (demoFile.txt) file
demo_file.write("This file is in read and write mode!")

Output:

fix the UnsupportedOperation not readable Error in Python

As you can see, the data is successfully written in the demoFile.txt file. Now let’s try to read the data in the same mode.

# open a file (demoFile.txt)
demo_file = open("demoFile.txt","r+")

# read the (demoFile.txt) file
print(demo_file.read())

# close the file
demo_file.close()

Output:

This file is in read and write mode!

Now the above codes are in r+ mode, and we are successfully writing and reading data from the file.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error

  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10
  • NameError: The OS Module Is Not Defined in Python
  • Python ValueError: Not Enough Values to Unpack
  • Resolve the TypeError: Can't Multiply Sequence by Non-Int of Type STR in Python