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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With