Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何修复使用with open()读取文件时的EOF错误_Python 3.x_Eof - Fatal编程技术网

Python 3.x 如何修复使用with open()读取文件时的EOF错误

Python 3.x 如何修复使用with open()读取文件时的EOF错误,python-3.x,eof,Python 3.x,Eof,我正在制作一个程序,从一组文件中读取一些内容,但我得到了一个EOF错误 def readMem(memId): with open(memId, "r") as rf: memVal = memId.read() 我使用open()在处得到错误(即使没有最后一行,错误仍然存在),但我无法找出原因。据我所知,没有问题,我甚至在另一个文件中使用了这个确切的代码。您应该调用file对象rf的read方法: def readMem(memId): with open(

我正在制作一个程序,从一组文件中读取一些内容,但我得到了一个EOF错误

def readMem(memId):

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

我使用open()在
处得到错误(即使没有最后一行,错误仍然存在),但我无法找出原因。据我所知,没有问题,我甚至在另一个文件中使用了这个确切的代码。

您应该调用file对象
rf
read
方法:

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

我无法在REPL中重现这一点,但在尝试调用
str
上的
read()
时,我确实遇到了另一个错误,您是否应该读取
rf
?@hoppeduppen但我应该有,如回答所述。谢谢