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