Thursday, July 9, 2020

Python Programming Practice for Class 12 ,How to Append and Delete Record from Binary File


binaryfilepractice - Copy
In [3]:
def updatenam(r,m):
    import pickle
    import os
    f=open('std.bat','rb')
    rl=[]
    while True:
        try:
            rec=pickle.load(f)
            rl.append(rec)
        except  EOFError:
            break
    f.close()
    for i in range(len(rl)):
            if rl[i][1]==r:
               rl[i][1]=m
    f=open('std.bat','wb')
    for x in rl:
            pickle.dump(x,f)
    f.close()
In [4]:
def deleterecord(r):
    import pickle
    import os
    f=open('std.bat','rb')
    rl=[]
    while True:
        try:
            rec=pickle.load(f)
            rl.append(rec)
        except  EOFError:
            break
    f.close()
    f=open('std.bat','wb')
    for i in range(len(rl)):
            if rl[i][1]==r:
                   continue
   
            pickle.dump(rl[i],f)
    f.close()
In [5]:
def stdentry(r,nam,c):# function to write a reord in file 'std.dat'
    
    import pickle
    f=open('std.bat','ab')
    pickle.dump([r,nam,c],f)
    f.close()
In [6]:
def stdshow():   # function designed for displaying all record of file std.bat
    f=open('std.bat','rb')
    import pickle
    
    f.seek(0,2)
    e=f.tell()
    f.seek(0,0)
    while(f.tell()<e):
        d=pickle.load(f)
        print(d)
    f.close()
In [7]:
def stdsearchnam(nam):  # writing function for searching record by name 
    f=open('std.bat','rb')
    import pickle
    
    f.seek(0,2)
    eof=f.tell()
    f.seek(0,0)
    while(f.tell()<eof):
        d=pickle.load(f)
        if(d[1]==nam):
            print('my name is',d[1],'my rollno is',d[0],'and my class is',d[2])
        
    f.close()
In [8]:
stdentry(10,'satish',12)
In [9]:
stdshow()
[2, 'ajit', 12]
[3, 'manoj', 12]
[1, 'agam', 12]
[10, 'satish', 12]
In [10]:
updatenam('satish','SATISH')
In [11]:
stdshow()
[2, 'ajit', 12]
[3, 'manoj', 12]
[1, 'agam', 12]
[10, 'SATISH', 12]
In [12]:
deleterecord('manoj')
In [13]:
stdshow()
[2, 'ajit', 12]
[1, 'agam', 12]
[10, 'SATISH', 12]
In [14]:
stdentry(15,'palak',12)
In [15]:
stdshow()
[2, 'ajit', 12]
[1, 'agam', 12]
[10, 'SATISH', 12]
[15, 'palak', 12]
In [ ]:
 

No comments:

Post a Comment