为什么我得不到支持

2024-03-29 11:31:12 发布

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

我是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

在这个语法上,我得到

^{pr2}$

我从在线教程中得到它,我正在使用python3。你知道什么会导致这种错误信息吗?在


Tags: thetotexttesttxtbytes语法open
1条回答
网友
1楼 · 发布于 2024-03-29 11:31:12

这是个打字错误。您已经打开了text_file,但是行

text_in_file = test_file.read()

想从关闭的test_file读取。将行改为:

^{pr2}$

相关问题 更多 >