[Tutor] Deleting items from a shelve file when items are unknown
James Austin
james.londonsw15 at gmail.com
Fri Oct 18 18:03:40 EDT 2019
Hello list
I am quite new to Python, so please bare with me.
I am developing a basic clipboard application that saves text to a
keyword and lists the saved items for the user. Users will then be able
to paste saved text into an external application by using the keyword
they first assigned. Saved data is stored in a Shelve file. I have a
working prototype for the 'save' and 'list' features of the program.
Currently this program is command line only.
Before adding the 'paste' feature, I am working on implementing a way
for users to delete keywords and text they no longer want. This is where
I hope the list can advise please. I am trying to write code that will
allow the user to delete **any** keyword that they find in the list. A
portion of my code is below and below that is a brief summary of what I
have tried so far:
---snip---
# Delete keywords/comments
elif len(sys.argv) == 2 and sys.argv[1] == 'delete':
if clipboardShelf[sys.argv[2]] in clipboardShelf.keys():
clipboardShelf.pop()
This code does not throw errors when tested with: python clipboard.py.
However, when I run python clipboard.py list, the keyword remains in the
shelve file.
Although my understanding is limited, I believe, from what I have read,
that shelve files act like dictionaries. However, using the 'del' option
yields the same result.
I have tried several variations including the following 'for' loop:
---snip---
elif len(sys.argv) == 2 and sys.argv[1] == 'delete':
for key in clipboardShelf.keys():
del clipboardShelf['']
I have also tried signifying the unknown key with empty brackets.
I hope I have provided enough context for someone to help. I suspect
that I have misunderstood something or am overthinking what may be a
very simple solution.
Thank you for any and all help offered.
Best wishes
James
More information about the Tutor
mailing list