Wednesday, July 8, 2020

Python Binary File operation Record Append,Display and Search Operation Program and Practice


binary file operation Record Append,Record Display,etc
In [6]:
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 [7]:
stdentry(1,'anukrati',12) #calling stdentry
In [8]:
stdentry(2,'prachi',12)   #calling stdentry
In [9]:
stdentry(3,'agam',12)    #calling stdentry
In [16]:
def stdshow():   # function designed for displaying all record of file std.bat
    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)
        print(d)
    f.close()
    
In [17]:
stdshow()   #calling stdshow() to display all record 
[1, 'anukrati', 12]
[2, 'prachi', 12]
[3, 'agam', 12]
In [18]:
stdshow()    #calling stdshow() to display all record 
[1, 'anukrati', 12]
[2, 'prachi', 12]
[3, 'agam', 12]
In [19]:
stdentry(4,'sonu',12)    #calling stdshow() to display all record 
In [20]:
stdshow()    #calling stdshow() to display all record 
[1, 'anukrati', 12]
[2, 'prachi', 12]
[3, 'agam', 12]
[4, 'sonu', 12]
In [21]:
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 [22]:
stdsearchnam('sonu')  #calling of function for search in file by name sonu
my name is sonu my rollno is 4 and my class is 12
In [24]:
stdsearchnam('agam')  #calling of function for search in file by name agam
my name is agam my rollno is 3 and my class is 12
In [25]:
stdsearchnam('prachi')  #calling of function for search in file by name prachi
my name is prachi my rollno is 2 and my class is 12
In [37]:
def stdsearchnam(nam,nam1):
    f=open('std.bat','rb+')
    import pickle
    
    f.seek(0,2)
    eof=f.tell()
    f.seek(0,0)
    while(f.tell()<eof):
        p=f.tell()
        d=pickle.load(f)
        if(d[1]==nam):
            d=[d[0],nam1,d[2]]
            f.seek(p,0)
            pickle.dump(d,f)
        
    f.close()
    
In [ ]:
 

No comments:

Post a Comment