2016-03-21 60 views
0

我正試圖將字典存儲到文件中。爲什麼我總是得到這個cPickle錯誤?

這是我的代碼部分:

Accounts={} 
if username not in Accounts: 
    Accounts[username]=password 
    database=open("my_mail_database", "w") 
    pickle.dump(Accounts, database) 
    database.close() 

,我總是得到這樣的錯誤:

Traceback (most recent call last): 
File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__ 
return self.func(*args) 
File "C:\Python34\python 3.4\my_gmail2.pyw", line 51, in submit_account 
pickle.dump(Accounts, database) 
TypeError: must be str, not bytes 

有人能告訴我什麼是我的代碼的問題?

+5

http://stackoverflow.com/questions/13906623/using-pickle-dump-typeerror-must-be-str-不是字節 – zezollo

回答

1

試一下:

Accounts={} 
if username not in Accounts: 
    Accounts[username]=password 
    database=open("my_mail_database", "wb") 
    pickle.dump(Accounts, database) 
    database.close() 

你要打開的文件與WB