Pickling is just a way to serialize a data structure. If you are using it for data storage it makes sense you would data = pickle.load(file), modify data, pickle.dump(data, file).
EXAMPLE 1
import pickle
animals = ["tiger", "lion", "giraffe"]
pickled = pickle.dumps(animals)
print(pickled)
things = pickle.loads(pickled)
animals = things + ["cow", "sheep", "pig"]
pickled = pickle.dumps(animals)
print(pickled)
things = pickle.loads(pickled)
print(things)
Output:
b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05tiger\x94\x8c\x04lion\x94\x8c\x07giraffe\x94e.'
b'\x80\x04\x952\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05tiger\x94\x8c\x04lion\x94\x8c\x07giraffe\x94\x8c\x03cow\x94\x8c\x05sheep\x94\x8c\x03pig\x94e.'
['tiger', 'lion', 'giraffe', 'cow', 'sheep', 'pig']
It does not make sense appending to a pickled file. You are concatenating pickles.
EXAMPLE 2
import pickle
animals = ["tiger", "lion", "giraffe"]
pickled = pickle.dumps(animals)
print(pickled)
things = pickle.loads(pickled)
print(things)
pickled = pickled + pickle.dumps(["cow", "sheep", "pig"])
print(pickled)
things = pickle.loads(pickled)
print(things)
Output:
b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05tiger\x94\x8c\x04lion\x94\x8c\x07giraffe\x94e.'
['tiger', 'lion', 'giraffe']
b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x05tiger\x94\x8c\x04lion\x94\x8c\x07giraffe\x94e.\x80\x04\x95\x19\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x03cow\x94\x8c\x05sheep\x94\x8c\x03pig\x94e.'
['tiger', 'lion', 'giraffe']
Appending to "pickled" makes it longer, but it is not properly formatted to make a longer list.
Or are you trying to save multiple data structures to a pickle file?
EXAMPLE 3
import pickle
jungle = ["tiger", "lion", "giraffe"]
farm = ["cow", "sheep", "pig"]
with open("test.bin", "wb") as file:
pickle.dump(jungle, file)
with open("test.bin", "ab") as file: # Appending to an existing pickle file
pickle.dump(farm, file)
with open("test.bin", "rb") as file:
while True:
try:
print(pickle.load(file)) # Need to call pickle.load() for each pickle.dump() used to make the file
except EOFError:
break
Output:
['tiger', 'lion', 'giraffe']
['cow', 'sheep', 'pig']
I don't see this as being particularly useful, but there may be a special case where this makes sense. Usually if you want to pickle multiple data structures you put them in a list and pickle the list.
EXAMPLE 4
import pickle
jungle = ["tiger", "lion", "giraffe"]
farm = ["cow", "sheep", "pig"]
with open("test.bin", "wb") as file:
pickle.dump([jungle, farm], file)
with open("test.bin", "rb") as file:
a, b = pickle.load(file)
print(a, b)
Output:
['tiger', 'lion', 'giraffe'] ['cow', 'sheep', 'pig']
Is Gribouillis the learned colleague? I found the link here:
https://python-forum.io/thread-38018.htm...ght=pickle
I don't know if this is one of the "special cases" where it makes sense to append to a "pickled" file. As shown in EXAMPLE 3, you'll need to call pickle.load() multiple times if you called pickle.dump() multiple times, or used pickle.dump() to append to an existing file.