Why am I getting unsupportedoperation: read


I am new to python and I am having an issue with the following syntax

  test_file = open("test.txt", "wb")
  test_file.write(bytes("Write me to the filen", 'UTF-8'))
  text_file = open("test.txt","r+")
  text_in_file = test_file.read() # this is where the error emerges
  # more code goes here

On this syntax, I am getting

io.UnsupportedOperation: read

I got it from an online tutorial and I am using python 3. do you know what could lead to such kind of error message?

Asked By: user8156106



It’s a typo. You have opened text_file, but the line

text_in_file = test_file.read()

wants to read from the closed test_file. Change the line to:

text_in_file = text_file.read()
Answered By: Heiko Oberdiek
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.