2017-06-18 54 views
2

我是新来的蟒蛇,我有问题的语法如下为什么我收到unsupportedoperation:读

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.你知道什么可能导致这样的错误信息?

+0

再次,这对于使用外部文件来说可能是基本的但非常重要 – Lucas

+0

您的代码中存在拼写错误。仔细阅读,看看你是否能够发现你拼错的名字。 –

回答

2

这是一个错字。你已经打开text_file,但行

text_in_file = test_file.read() 

希望从封闭test_file阅读。将该行更改为:

text_in_file = text_file.read() 
相关问题