In [33]:
import pickle

In [34]:
a = ['test', 'test2', 'test3']
a


Out[34]:
['test', 'test2', 'test3']

open file


In [35]:
fileName = "pickleTest"

In [36]:
fileObject = open(fileName, 'wb')

save object to file


In [37]:
pickle.dump(a, fileObject)

In [38]:
fileObject.close()

read from file


In [39]:
fObject = open(fileName, 'rb')

In [40]:
type(fObject)


Out[40]:
_io.BufferedReader

In [41]:
fileName


Out[41]:
'pickleTest'

In [42]:
b = pickle.load(fObject)

In [43]:
fObject.close()

In [44]:
b


Out[44]:
['test', 'test2', 'test3']

In [45]:
a == b


Out[45]:
True

easier way to handle opening and closing file


In [46]:
with open(fileName, 'rb') as pfile:
    c = pickle.load(pfile)

In [47]:
c


Out[47]:
['test', 'test2', 'test3']

adding multiple data structures


In [48]:
d = ['testa', 'testb', 'testb']
d


Out[48]:
['testa', 'testb', 'testb']

In [49]:
with open(fileName, 'ab') as pfile:
    pickle.dump(d, pfile)

In [56]:
with open(fileName, 'rb') as pfile:
    e = pickle.load(pfile)
    f = pickle.load(pfile)

In [53]:
e


Out[53]:
['test', 'test2', 'test3']

In [54]:
f


Out[54]:
['testa', 'testb', 'testb']

In [ ]: