Issue
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 file\n", 'UTF-8'))
test_file.close()
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?
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.