1
我想從這裏使用下面的代碼: How can I save all the variables in the current python session?擱置代碼給出KeyError異常
import shelve
T='Hiya'
val=[1,2,3]
filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'n') # 'n' for new
for key in dir():
try:
my_shelf[key] = globals()[key]
except TypeError:
#
# __builtins__, my_shelf, and imported modules can not be shelved.
#
print('ERROR shelving: {0}'.format(key))
my_shelf.close()
但它提供了以下錯誤:
Traceback (most recent call last):
File "./bingo.py", line 204, in <module>
menu()
File "./bingo.py", line 67, in menu
my_shelf[key] = globals()[key]
KeyError: 'filename'
你能幫助我嗎?
謝謝!
明顯'全局()'和'目錄()'是不是一回事,爲什麼你要循環一個,並索引另一個? – 2012-01-13 22:10:29