Pickle dump replaces current file data

Viewed 28015

When I use pickle, it works fine and I can dump any load.

The problem is if I close the program and try to dump again, it replaces the old file data with the new dumping. Here is my code:

import pickle
import os
import time


dictionary = dict()


def read():
    with open('test.txt', 'rb') as f:
        a = pickle.load(f)
    print(a)
    time.sleep(2)


def dump():
    chs = raw_input('name and number')
    n = chs.split()
    dictionary[n[0]] = n[1]
    with open('test.txt', 'wb') as f:
        pickle.dump(dictionary, f)


Inpt = raw_input('Option : ')
if Inpt == 'read':
    read()
else:
    dump()
2 Answers
Related