102

I have this code:

line1 = []
line1.append("xyz ")
line1.append("abc")
line1.append("mno")
    
file = open("File.txt","w")
for i in range(3):
    file.write(line1[i])
    file.write("\n")

for line in file:
    print(line)
file.close()

But when I try it, I get an error message like:

  File "...", line 18, in <module>
     for line in file:
 
UnsupportedOperation: not readable

Why? How do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sachin Patil
  • 1,043
  • 2
  • 8
  • 8

6 Answers6

240

You are opening the file as "w", which stands for writable.

Using "w" you won't be able to read the file. Use the following instead:

file = open("File.txt", "r")

Additionally, here are the other options:

"r"   Opens a file for reading only.
"r+"  Opens a file for both reading and writing.
"rb"  Opens a file for reading only in binary format.
"rb+" Opens a file for both reading and writing in binary format.
"w"   Opens a file for writing only.
"a"   Open for writing. The file is created if it does not exist.
"a+"  Open for reading and writing.  The file is created if it does not exist.
Neuron
  • 5,141
  • 5
  • 38
  • 59
Sreetam Das
  • 3,226
  • 2
  • 22
  • 36
34

Use a+ to open a file for reading, writing and create it if it doesn't exist.

a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. -Python file modes

with open('"File.txt', 'a+') as file:
    print(file.readlines())
    file.write("test")

Note: opening file in a with block makes sure that the file is properly closed at the block's end, even if an exception is raised on the way. It's equivalent to try-finally, but much shorter.

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
11

There are few modes to open file (read, write etc..)

If you want to read from file you should type file = open("File.txt","r"), if write than file = open("File.txt","w"). You need to give the right permission regarding your usage.

more modes:

  • r. Opens a file for reading only.
  • rb. Opens a file for reading only in binary format.
  • r+ Opens a file for both reading and writing.
  • rb+ Opens a file for both reading and writing in binary format.
  • w. Opens a file for writing only.
  • you can find more modes in here
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
5

This will let you read, write and create the file if it don't exist:

f = open('filename.txt','a+')
f = open('filename.txt','r+')

Often used commands:

f.readline() #Read next line
f.seek(0) #Jump to beginning
f.read(0) #Read all file
f.write('test text') #Write 'test text' to file
f.close() #Close file
Punnerud
  • 7,195
  • 2
  • 54
  • 44
1

Sreetam Das's table is nice but needs a bit of an update according to w3schools and my own testing. Unsure if this due to the move to python 3.

"a" - Append - will append to the end of the file and will create a file if the specified file does not exist.

"w" - Write - will overwrite any existing content and will create a file if the specified file does not exist.

"x" - Create - will create a file, returns an error if the file exist.

I would have replied directly but I do not have the rep.

https://www.w3schools.com/python/python_file_write.asp

Jeff Mitchell
  • 1,067
  • 1
  • 8
  • 16
Djok
  • 11
  • 6
0

Here is an example of how to open a file in read mode and read its content:

try:

    file_obj = open('file.txt', 'r')
    content = file_obj.read()
    print(content)
    file_obj.close()
except FileNotFoundError:
    print("File does not exist.")
except IOError:
    print("Error reading file.")

In this example, we open the ‘file.txt’ file in read mode using the ‘r’ parameter and read its contents using the ‘read()’ method. We then print the contents and close the file object. If the file does not exist, or there is an error reading the file, we print an appropriate error message.

READ MORE: Character ai chat error please try again Other solutions Another possible solution is to use the ‘with’ statement when opening and reading a file. This way, the file object will be automatically closed when we are done with it, and we don’t have to worry about closing it ourselves. Here is an example:

try:

    with open('file.txt', 'r') as file_obj:
        content = file_obj.read()
        print(content)
except FileNotFoundError:
    print("File does not exist.")
except IOError:
    print("Error reading file.")

In this example, we use the ‘with’ statement to open the ‘file.txt’ file in read mode and read its contents using the ‘read()’ method. We then print the contents. If the file does not exist, or there is an error reading the file, we print an appropriate error message.

Another possible cause of the ‘io.unsupportedoperation: not readable’ error is when we pass a string instead of a file object to a function that expects a file object. For example, if we do the following:

filename = 'file.txt'
content = function_that_expects_file_object(filename)

We will get the ‘io.unsupportedoperation: not readable’ error because we are passing a string instead of a file object. To fix this, we need to open the file and pass the file object to the function:

try:

    with open('file.txt', 'r') as file_obj:
        content = function_that_expects_file_object(file_obj)
        print(content)
except FileNotFoundError:
    print("File does not exist.")
except IOError:
    print("Error reading file.")

In this example, we open the ‘file.txt’ file in read mode using the ‘with’ statement and pass the file object to the ‘function_that_expects_file_object()’ function. We then print the contents. If the file does not exist, or there is an error reading the file, we print an appropriate error message. See the details: https://letchatgpt.com/io-unsupportedoperation-not-readable/

Kim Marry
  • 55
  • 3