我在教自己如何使用Python3。我想训练我获得的技能并编写命令行备份程序。我正在尝试使用Shelve
模块保存默认备份并保存位置,但似乎它会在我关闭或重新启动程序时忘记我保存的变量。
这是与货架配合使用的主要功能:
def WorkShelf(key, mode='get', variable=None):
"""Either stores a variable to the shelf or gets one from it.
Possible modes are 'store' and 'get'"""
config = shelve.open('Config')
if mode.strip() == 'get':
print(config[key])
return config[key]
elif mode.strip() == 'store':
config[key] = variable
print(key,'holds',variable)
else:
print("mode has not been reconginzed. Possible modes:\n\t- 'get'\n\t-'store'")
config.close()
因此,每当我调用此函数来存储变量并在此之后调用函数时,它就能完美地运行。我试图手动访问货架,一切都在那里。 这是用于存储变量的代码:
WorkShelf('BackUpPath','store', bupath)
WorkShelf('Path2BU', 'store', path)
当我在重新启动脚本后尝试从架子上获取变量时出现问题。这段代码:
config = shelve.open('Config')
path = config['Path2BU']
bupath = config['BackUpPath']
给我这个错误:
Traceback (most recent call last):
File "C:\Python35-32\lib\shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'Path2BU'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
config['Path2BU']
File "C:\Python35-32\lib\shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
File "C:\Python35-32\lib\dbm\dumb.py", line 141, in __getitem__
pos, siz = self._index[key] # may raise KeyError
KeyError: b'Path2BU
基本上,这是我可以通过调用ShelveObject ['ThisKeyDoesNotExist']重现的错误。
我现在真的迷失了。当我尝试手动创建一个架子,关闭它并再次访问它似乎工作(即使我之前有错误)现在。我已经阅读了关于这个的每一篇文章,我想到了书架腐败(但它不太可能每次都发生),而且我现在已经读过我的剧本A到Z大约20次了。
感谢您的帮助(我希望这次能以正确的方式提出我的问题)!
修改
好的,这让我发疯了。隔离WorkShelf()确实可以正常工作,如下所示:
import shelve
def WorkShelf(key, mode='get', variable=None):
"""Either stores a variable to the shelf or gets one from it.
Possible modes are 'store' and 'get'"""
config = shelve.open('Config')
if mode.strip() == 'get':
print(config[key])
return config[key]
elif mode.strip() == 'store':
config[key] = variable
print(key,'holds',variable)
else:
print("mode has not been reconginzed. Possible modes:\n\t- 'get'\n\t-'store'")
config.close()
if False:
print('Enter path n1: ')
path1 = input("> ")
WorkShelf('Path1', 'store', path1)
print ('Enter path n2: ')
path2 = input("> ")
WorkShelf('Path2', 'store', path2)
else:
path1, path2 = WorkShelf('Path1'), WorkShelf('Path2')
print (path1, path2)
没问题,完美。
但是当我在我的脚本中使用相同的函数时,我得到了这个输出。它基本上告诉我确实将变量写入搁置文件('此键保存此变量'消息)。我甚至可以使用重启时使用的相同代码来调用它们。但是在程序重置后调用它们时,就像'你在说什么?我从来没有救过这些'。
Welcome to this backup manager.
We will walk you around creating your backup and backup preferences
What directory would you like to backup?
Enter path here: W:\Users\Damien\Documents\Code\Code Pyth
Would you like to se all folders and files at that path? (enter YES|NO)
> n
Okay, let's proceed.
Where would you like to create your backup?
Enter path here: N:\
Something already exists there:
19 folders and 254 documents
Would you like to change your location?
> n
Would you like to save this destination (N:\) as your default backup location ? That way you don't have to type it again.
> y
BackUpPath holds N:\
If you're going to be backing the same data up we can save the files location W:\Users\Damien\Documents\Code\Code Pyth so you don't have to type all the paths again.
Would you like me to remember the backup file's location?
> y
Path2BU holds W:\Users\Damien\Documents\Code\Code Pyth
>>>
======== RESTART: W:\Users\Damien\Documents\Code\Code Pyth\Backup.py ========
Welcome to this backup manager.
Traceback (most recent call last):
File "C:\Python35-32\lib\shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'Path2BU'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "W:\Users\Damien\Documents\Code\Code Pyth\Backup.py", line 198, in <module>
path, bupath = WorkShelf('Path2BU'), WorkShelf('BackUpPath')
File "W:\Users\Damien\Documents\Code\Code Pyth\Backup.py", line 165, in WorkShelf
print(config[key])
File "C:\Python35-32\lib\shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
File "C:\Python35-32\lib\dbm\dumb.py", line 141, in __getitem__
pos, siz = self._index[key] # may raise KeyError
KeyError: b'Path2BU'
请帮助,我疯了,可能会开发一个类来存储和从文本文件中获取变量。
答案 0 :(得分:0)
好的,我刚刚发现了什么问题。
我的设置代码中有一个os.chdir()。每当安装完成并且我想打开我的配置文件时,它会查看当前目录,但是货架位于设置中指向的目录os.chdir()中。
由于某种原因,我最终在目录中的空架子上实际应该是。这花了我一天的调试。
这对今天的人来说都是如此!