Binary File Program
Binary File Program
Source code :
#To perform different operations in binary file
import pickle
#to insert data in form of records
def insert():
a=[]
while True:
r=int(input('enter Rollno:- '))
n=input('enter name :- ')
m=float(input("enter marks:- "))
l=[r,n,m]
a.append(l)
q=input("do you want to enter more records?(y/n) :- ")
if q in 'nN':
break
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'wb')
pickle.dump(a,f)
print("data has been inserted in form of records")
f.close()
#to display all the records
def display():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
w=pickle.load(f)
print('the records are: ')
for i in w:
print(i)
f.close()
c=1
if c==0:
print("record not found ")
print("the new record is:", v)
f.close()
#to copy all the data into another file
def copy():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
f2=open("C:\\Users\\pc\\Desktop\\csc\\new-records.txt",'wb')
h=pickle.load(f)
l=[]
for i in h:
l.append(i)
pickle.dump(l,f2)
print("new file is created with name new-records and record transfered")
f.close()
f2.close()
#to copy data of students having marks>90.
def copy90():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
f2=open("C:\\Users\\pc\\Desktop\\csc\\new90-records.txt",'wb')
h=pickle.load(f)
l=[]
for i in h:
if i[2]>90:
l.append(i)
pickle.dump(l,f2)
print("new file is created with name "new90-records" and record of student having
marks>90 transfered")
f.close()
f2.close()
#to copy data of students have name starting with 'A'
def copyA():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
f2=open("C:\\Users\\pc\\Desktop\\csc\\newA-records.txt",'wb')
h=pickle.load(f)
l=[]
for i in h:
if i[1][0] in 'aA':
l.append(i)
pickle.dump(l,f2)
print("new file is created with name newA-records and record of student with name
starting with A transfered")
f.close()
f2.close()
#to display average marks of all students in the record
def average():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
h=pickle.load(f)
a=0
c=0
for i in h:
a+=i[2]
c+=1
avg = a/c
print("average marks: ", avg)
f.close()
output:-
select the desired option :
1.insert data in form of records
2.display all the records
3.search and display particular record
4.search and display record to the corresponding name
5.modify the record
6.copy the data to newfile
7.copy data of students having marks > 90
8.copy data of students having name starting with A
9.Display average marks
10.Display total no. of records
11.delete an record
12.exit