python shelve 不保存/加载
当我通过子文件中的函数保存/加载工作区时,搁置不起作用(test1)。 但是,如果我在一个文件中执行相同的操作,它就会起作用(test2)。这是为什么?如何解决第一种情况的问题?
在主文件中:
# in saveWS.py
# to save
def saveSlv(fileName):
destination='./'+fileName+'_shelve.pkl'
bk = shelve.open(destination,'n')
for k in dir():
try:
bk[k] = globals()[k]
except Exception:
pass
bk.close()
# to restore
def loadSlv(fileName):
myshelve ='./'+fileName+'_shelve.pkl'
bk_restore = shelve.open(myshelve)
for k in bk_restore:
globals()[k] = bk_restore[k]
bk_restore.close()
在主文件中:
import shelve
# User defined functions
from saveWS import saveSlv, loadSlv
# It doesn't work
a=1,2,3
b='ypk'
fileName='test1'
# save the variables in work space by calling a function
saveSlv(fileName)
del a, b
# restore the work space by calling a function
loadSlv(fileName)
# It works
a=1,2,3
b='ypk'
fileName='test2'
# save the variables in work space
destination='./'+fileName+'_shelve.pkl'
bk = shelve.open(destination,'n')
for k in dir():
try:
bk[k] = globals()[k]
except Exception:
pass
bk.close()
del a, b
# restore the work space
myshelve ='./'+fileName+'_shelve.pkl'
bk_restore = shelve.open(myshelve)
for k in bk_restore:
globals()[k] = bk_restore[k]
bk_restore.close()
When I save/load my workspace via functions in a subfile, shelve doesn't work (test1).
However, if I do the same in one file, it works (test2). Why is that? How can I fix the problem for the first case?
In the main file:
# in saveWS.py
# to save
def saveSlv(fileName):
destination='./'+fileName+'_shelve.pkl'
bk = shelve.open(destination,'n')
for k in dir():
try:
bk[k] = globals()[k]
except Exception:
pass
bk.close()
# to restore
def loadSlv(fileName):
myshelve ='./'+fileName+'_shelve.pkl'
bk_restore = shelve.open(myshelve)
for k in bk_restore:
globals()[k] = bk_restore[k]
bk_restore.close()
In the main file:
import shelve
# User defined functions
from saveWS import saveSlv, loadSlv
# It doesn't work
a=1,2,3
b='ypk'
fileName='test1'
# save the variables in work space by calling a function
saveSlv(fileName)
del a, b
# restore the work space by calling a function
loadSlv(fileName)
# It works
a=1,2,3
b='ypk'
fileName='test2'
# save the variables in work space
destination='./'+fileName+'_shelve.pkl'
bk = shelve.open(destination,'n')
for k in dir():
try:
bk[k] = globals()[k]
except Exception:
pass
bk.close()
del a, b
# restore the work space
myshelve ='./'+fileName+'_shelve.pkl'
bk_restore = shelve.open(myshelve)
for k in bk_restore:
globals()[k] = bk_restore[k]
bk_restore.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
globals()
-你的问题的相关部分(我猜):因此函数中的
globals()
始终是命名空间saveWS.py
。以及此处
dir()
-相关一:因此,
dir()
指的是函数内的本地命名空间。您可能可以通过传递
dir()
和globals()
作为参数来解决这个问题:调用:
您可以通过使用
而不 使用
variables
参数来解决这个问题(导入sys
之后)。在您的用例中,您可能可以进一步将namespace['__name__']
替换为'__main__'
。如果您想摆脱所有附加参数,您可以尝试如果这对您有用:检查 这里如果合适的话。
Here's the
globals()
-related part of your problem (I guess):So
globals()
in your functions is always the namespace ofsaveWS.py
.And here the
dir()
-related one:Therefore
dir()
refers to the local namespace within the function.You probably could fix that by passing
dir()
andglobals()
as arguments:Calling:
You could do without the
variables
argument by usinginstead (after importing
sys
). In your usecase you could probably further replacenamespace['__name__']
with'__main__'
. If you want to get rid of all the additional arguments you could try if this works for you:Check here if that's appropriate.