import shelve a = shelve.open('1') b = [1,2,3] a['b'] = b a.close()a['b'] Traceback (most recent call last):File "C:\Users\Administrator\AppData\Local ...
import shelve
a = shelve.open('1')
b = [1,2,3]
a['b'] = b
a.close()
a['b']
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'b'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 70, in closed
raise ValueError('invalid operation on closed shelf')
ValueError: invalid operation on closed shelf
原因是a.close()就已經關閉了shelf文件。
1 >>> shelve.open('1')['b'] 2 [1,2,3] 3 >>> shelve.close() 4 Traceback (most recent call last): 5 File "<stdin>", line 1, in <module> 6 AttributeError: module 'shelve' has no attribute 'close'
shelve模塊沒有close(),需要變數來關閉。
那如果沒有關閉會怎樣?
1 >>> b 2 [1, 2, 3] 3 >>> c = shelve.open('5') 4 >>> c['b'] = b 5 >>> c['b'] 6 [1, 2, 3] #創建並打開文件5 7 >>> d = shelve.open('6') 8 >>> d['b'] = [4,5,6] 9 >>> d['b'] 10 [4, 5, 6] #創建並打開文件6 11 >>> b 12 [1, 2, 3] 13 >>> c['b'] 14 [1, 2, 3] 15 >>> d.close() 16 >>> d['b'] #關閉文件 17 Traceback (most recent call last): 18 File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 111, in __getitem__ 19 value = self.cache[key] 20 KeyError: 'b' 21 22 During handling of the above exception, another exception occurred: 23 24 Traceback (most recent call last): 25 File "<stdin>", line 1, in <module> 26 File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 113, in __getitem__ 27 f = BytesIO(self.dict[key.encode(self.keyencoding)]) 28 File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 70, in closed 29 raise ValueError('invalid operation on closed shelf') 30 ValueError: invalid operation on closed shelf 31 >>> c['b'] 32 [1, 2, 3]
從上面代碼可以看出shelve模塊的close()是分別針對每個文件的,會一直處於打開狀態直到關閉。
1 >>> list(c.values()) 2 [[1, 2, 3]] 3 >>> list(c.keys()) 4 ['b'] 5 >>> list(c) 6 ['b']
shelf值預設返回值為keys()方法的返回值。