Let\'s say I have a dictionary and I want to write it into an existing file. How can I do so without losing anything that could potentially already exist in the file? What I
pickle may be another choice:
import pickle
output = open('output.txt', 'ab+')
data = {'a': [1, 2, 3],}
pickle.dump(data, output)
output.close()
# read data
output = open('output.txt', 'rb')
obj_dict = pickle.load(output) # 'obj_dict' is a dict object
But only the data that has been serialized by pickle could be read using pickle.load. So if you want to read all data from the file, you should pickle.dump all the data into the file.