2017-06-18 24 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() 
相關問題