我无法使用上下文管理器(with)读取文件

2024-10-01 22:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

使用以下代码:

with open("sd1file.txt",'r') as testfile:
    file_contents = testfile.read
    print (file_contents)

我得到这个错误

<built-in method read of _io.TextIOWrapper object at 0x02EEB4B0>

我可以用 file = open(testfile,'r')等没有问题 i、 e.文件存在且可访问,即我可以对其进行读写

有什么模块需要导入吗


Tags: 代码intxtreadas错误withcontents

热门问题