Write a program to delete the content provided by user from the binary file. The file is very large and can’t fit in computer’s memory.


In [2]:
import pickle
import os
def deleteContent(filename):
    '''Function to delete content provided by user from a binary (pickle) file which is too large to fit in memory'''
    #Assuming multiple dumps
    choice = raw_input('Do you want to enter content to delete Y/N')
    if choice.lower()=='y':
        content = input('Enter content to delete: ')
        f = open(filename,'rb')
        t = open('temp.b','wb')
        while True:
            try:
                a = pickle.load(f)
                if a != content:
                    pickle.dump(a,t)
            except EOFError:
                print 'Done'
        f.close()
        t.close()
        os.remove(filename)
        os.rename('temp.b',filename)
    elif choice.lower() == 'n':
        f = open(filename,'rb')
        t = open('temp.b','wb')
        while True:
            try:
                a = pickle.load(f)
                delete = raw_input('Do you want to delete: ' + str(a) + 'Y/N')
                if delete.lower()=='n':
                    pickle.dump(a,t)
                elif delete.lower()=='y':
                    pass
            except EOFError:
                print 'Done'
        f.close()
        t.close()
        os.remove(filename)
        os.rename('temp.b',filename)

In [ ]: