Dumping pickle is not working with rb+
<p>Why pickle file is not modified? But after I uncomment the line it works?</p>

<pre><code>with open(PATH, "rb+") as fp:
    mocks_pickle = pickle.load(fp)
    mocks_pickle['aa'] = '123'
    # pickle.dump(mocks_pickle, open(PATH, 'wb'))
    pickle.dump(mocks_pickle, fp)
</code></pre>

<p>You need to seek to the beginning of the file with <code>fp.seek(0)</code> before dumping the object.</p>

<p>If you don't seek you append the new pickle to the end of the file.
And when you <code>pickle.load</code> from the file you only get the first there is in the file.</p>

<pre><code>with open(PATH, "rb+") as fp:
    mocks_pickle = pickle.load(fp)
    mocks_pickle['aa'] = '123'
    fp.seek(0)
    pickle.dump(mocks_pickle, fp)
</code></pre>

python pickle