2016-03-21 79 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