私は自分自身にPython3を教えています。私は取得したスキルを訓練し、コマンドラインのバックアッププログラムを作成したかったのです。私はデフォルトのバックアップを保存して、Shelve
モジュールで場所を保存しようとしていますが、プログラムを終了または再起動するたびに保存する変数を忘れてしまっているようです。ここで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']を呼び出すことによって再現することができ、エラーです。
私はです。本当には今すぐ失われています。私が手作業で棚を作成しようとすると、それを閉じてもう一度アクセスしてみると、(以前はエラーがあったとしても)動作しているようです。私はこれに関するすべての記事を読んだことがありますが、私は棚の破損について考えていましたが(毎回起こるとは限りません)、今は20回ほどスクリプトAからZを読んでいます。
ご協力いただきありがとうございます。
EDIT
わかりましたので、これは私が狂気作っています。 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)
問題はありません。
しかし、スクリプトで同じ関数を使用すると、この出力が得られます。 はであり、シェルフファイルに変数を書き込むことができます( 'このキーはこの変数を保持しています'というメッセージ)。私は再起動時に使用したのと同じコードで呼び出すこともできます。しかし、プログラムをリセットした後にそれらを呼び出すと、「あなたはm8について何を話していますか?私はこれらを保存したことはありません。
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'
私は夢中になり、テキストファイルから変数を保存したり取得したりすることができます。