-1

Refer to this program as File A:

vocabulary = []
while True:
   user_input = input('You: ')
   if user_input == 'vocabulary':
      print(vocabulary)
   vocabulary.append(user_input)

The thing is, I want the information being appended to vocabulary to be permanent, and not emptied every time I run the program. How can I do this?

2 Answers2

1

Store vocabulary as an external file. Python's native filetype for storing and saving Python objects is a .pkl file (pickle) using the Pickle module.

I added an external file to your code sample, pickling the vocabulary variable.

import pickle, os

if os.path.isfile('pkl.pkl'):
    with open('pkl.pkl','rb') as p:
        vocabulary = pickle.load(p)
else:
    vocabulary = []
while True:
    user_input = input('You: ')
    if user_input == 'vocabulary':
        print(vocabulary)
    vocabulary.append(user_input)
    with open('pkl.pkl', 'wb') as p:
        pickle.dump(vocabulary, p)
mttpgn
  • 327
  • 6
  • 17
-1

You can use pickle to save your object (How to pickle a list?) and then modifying it again etc.

cccnrc
  • 1,195
  • 11
  • 27
  • This can not be a possible answer. It doesn't provide any valuable information rather than pointing at another question. You should make this a comment at best. – Unni Mar 26 '19 at 04:18