通过搁置模块引发KeyError将所有python变量保存在文件中

问题描述

我遵循主题How to save all the variables in the current python session?将所有python变量保存在文件中。

我做了以下代码:

import shelve

def saveWorkspaceVariables(pathSavedVariables):
    # This functions saves all the variables in a file.
    my_shelf = shelve.open(pathSavedVariables,'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()
    
T="test"

saveWorkspaceVariables("file.out")

但是,它会引发:KeyError: 'my_shelf'

为什么呢?如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)