Sunday, October 30, 2022

[FIXED] How to fix EOF Error when reading a file using with open()

Issue

I am making a program to read something from a group of files, but I get an EOF error.

def readMem(memId):

    with open(memId, "r") as rf:
        memVal = memId.read()

I get the error at the with open(), (error persists even without the last line) but I cannot find out why. In my knowledge, there is no issue, I have even used this EXACT code in a different file.


Solution

You should call the read method of the file object rf instead:

def readMem(memId):
    with open(memId, "r") as rf:
        memVal = rf.read()


Answered By - blhsing
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.