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()