gpt4 book ai didi

python - 为什么我收到不支持的操作 : read

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:29 25 4
gpt4 key购买 nike

我是 python 新手,我遇到以下语法问题

  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

根据这个语法,我得到了

io.UnsupportedOperation: read

我从在线教程中得到它,并且我使用的是 python 3。你知道什么会导致这种错误消息吗?

最佳答案

这是一个错字。您已打开 text_file,但该行

text_in_file = test_file.read()

想要读取已关闭的test_file。将行更改为:

text_in_file = text_file.read()

关于python - 为什么我收到不支持的操作 : read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44610881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com