You could try the following:
import pickle
load_written_things = open("written_things.pkl", "rb")
added_stuff = raw_input("write: ")
written_things = pickle.load(load_written_things) + added_stuff
pickle.dump(written_things, open("written_things.pkl", "wb"))
print (written_things)
Update for the 2nd question:
Well let's say that you have dumped an initial list in 'written_things.pkl' like so:
#!/usr/bin/env python
import pickle
# initial list dumped in 'written_things.pkl'
alist = ['name1', 'name2', 'name3']
with open('written_things.pkl', 'wb') as f: pickle.dump(alist,f)
Now if I understood correctly you want to update this list with new items and dump the new list/result in 'written_things.pkl' using pickle
.
A way to do that would be the following:
# old stuff
load_written_things = open("written_things.pkl", "rb")
old = pickle.load(load_written_things)
new_item = raw_input('>> New item: ')
with open("written_things.pkl", "w") as f:
pickle.dump(old+[new_item], f)
# to read back the data
with open("written_things.pkl", "rb") as f:
data = pickle.load(f)
Also have in mind that in this way we add one item at a time. So if you want to enter multiple items at once, you could enter your items like this:
item1,item2,item3
etc... (or any other separator) and then replace the line:
pickle.dump(old+[new_item], f)
--with--> pickle.dump(old+new_item.split(','), f)
Also another stuff to consider as mentioned here, is that the pickle module is not secure against erroneous or maliciously constructed data.
That means that you must be sure that the data in the .pkl file are not malicious. There are many techniques in the wild that allow arbitrary code execution with python's pickles.
As just an alternative to pickle
you can always put your output (may be a list, dictionary etc...) in a file and then use ast.literal_eval
to read back your data. As mentioned in the module itself and also here,
ast.literal_eval
is considered safe to use.