0% found this document useful (0 votes)
6 views

Binary File Program

Cs

Uploaded by

aaditya160807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Binary File Program

Cs

Uploaded by

aaditya160807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Binary file

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()

#to search and display for a particular record


def search():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
v=pickle.load(f)
x=int(input("enter the rollno. of record to search:"))
c=0
for i in v:
if i[0]==x:
print("the Record is : ",i)
c=1
if c==0:
print("record not found ")
f.close()
#to search and display record by name
def searchname():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
v=pickle.load(f)
x=input("enter the name of record to search:")
c=0
for i in v:
if i[1]==x:
print("the Record is : ",i)
c=1
if c==0:
print("record not found ")
f.close()
#to modify a record
def modify():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
v=pickle.load(f)
x=int(input("enter the rollno. of record to modify:"))
c=0
for i in v:
if i[0]==x:
a=input("press 'n' th modify name and 'm' to modify marks" )
if a=='n' or a=='N':
n=input("enter new name: ")
i[1]=n
print("record updated")
elif a=='m' or a=='M':
n=input("enter new marks: ")
i[2]=n
print("record updated")

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()

#to display total no. of students in the record


def total():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb')
h=pickle.load(f)
c=0
for i in h:
c+=1
print("total students: ",c)
f.close()
#to delete a particular record
def delete():
f=open("C:\\Users\\pc\\Desktop\\csc\\student-records.txt",'rb+')
h=pickle.load(f)
x=int(input("enter the rollno. of record to delete: "))
a=0
l=[]
for i in h:
if i[0]!=x:
l.append(i)
a=1
if a==0:
print("record not found ")
pickle.dump(l,f)
print("the new record is:", l)
f.close()
#menu
while True:
print()
print("select the desired option : ")
print("1.insert data in form of records")
print("2.display all the records")
print("3.search and display particular record")
print("4.search and display record to the corresponding name")
print("5.modify the record")
print("6.copy the data to newfile")
print("7.copy data of students having marks > 90 ")
print("8.copy data of students having name starting with A")
print("9.Display average marks")
print("10.Display total no. of records")
print("11.delete an record")
print("12.exit")
print()
a= int(input("enter your choice:- "))
if a==1:
insert()
elif a==2:
display()
elif a==3:
search()
elif a==4:
searchname()
elif a==5:
modify()
elif a==6:
copy()
elif a==7:
copy90()
elif a==8:
copyA()
elif a==9:
average()
elif a==10:
total()
elif a==11:
delete()
elif a==12:
print("exiting this program....")
break
else:
print("invalid choice")

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

enter your choice:- 1


enter Rollno:- 1
enter name :- alok
enter marks:- 92
do you want to enter more records?(y/n) :- y
enter Rollno:- 2
enter name :- aditya
enter marks:- 99
do you want to enter more records?(y/n) :- y
enter Rollno:- 3
enter name :- aksath
enter marks:- 85
do you want to enter more records?(y/n) :- y
enter Rollno:- 4
enter name :- divya
enter marks:- 87
do you want to enter more records?(y/n) :- y
enter Rollno:- 5
enter name :- hemmant
enter marks:- 98
do you want to enter more records?(y/n) :- y
enter Rollno:- 6
enter name :- tanvi
enter marks:- 74
do you want to enter more records?(y/n) :- y
enter Rollno:- 7
enter name :- prajwal
enter marks:- 60
do you want to enter more records?(y/n) :- y
enter Rollno:- 8
enter name :- ritika
enter marks:- 79
do you want to enter more records?(y/n) :- y
enter Rollno:- 9
enter name :- smarthak
enter marks:- 90
do you want to enter more records?(y/n) :- y
enter Rollno:- 10
enter name :- mayank
enter marks:- 75
do you want to enter more records?(y/n) :- n
data has been inserted in form of records

enter your choice:- 2


the records are:
[1, 'alok', 92.0]
[2, 'aditya', 99.0]
[3, 'aksath', 85.0]
[4, 'divya', 87.0]
[5, 'hemmant', 98.0]
[6, 'tanvi', 74.0]
[7, 'prajwal', 60.0]
[8, 'ritika', 79.0]
[9, 'smarthak', 90.0]
[10, 'mayank', 75.0]

enter your choice:- 3


enter the rollno. of record to search:5
the Record is : [5, 'hemmant', 98.0]

enter your choice:- 4


enter the name of record to search:smarth
record not found
enter your choice:- 4
enter the name of record to search:smarthak
the Record is : [9, 'smarthak', 90.0]

enter your choice:- 5


enter the rollno. of record to modify:10
press 'n' th modify name and 'm' to modify marksm
enter new marks: 76
record updated
the new record is: [[1, 'alok', 92.0], [2, 'aditya', 99.0], [3, 'aksath', 85.0], [4, 'divya', 87.0], [5,
'hemmant', 98.0], [6, 'tanvi', 74.0], [7, 'prajwal', 60.0], [8, 'ritika', 79.0], [9, 'smarthak', 90.0],
[10, 'mayank', '76']]

enter your choice:- 6


new file is created with name “new-records” and record transfered

enter your choice:- 7


new file is created with name “new90-records” and record of student having marks>90
transfered

enter your choice:- 8


new file is created with name “newA-record” and record of student with name starting with
A transfered

enter your choice:- 9


average marks: 83.9

enter your choice:- 10


total students: 10

enter your choice:- 5


enter the rollno. of record to modify:7
press 'n' th modify name and 'm' to modify marksn
enter new name: naksh
record updated
the new record is: [[1, 'alok', 92.0], [2, 'aditya', 99.0], [3, 'aksath', 85.0], [4, 'divya', 87.0], [5,
'hemmant', 98.0], [6, 'tanvi', 74.0], [7, 'naksh', 60.0], [8, 'ritika', 79.0], [9, 'smarthak', 90.0],
[10, 'mayank', 75.0]]
enter your choice:- 11
enter the rollno. of record to delete: 1
the new record is: [[2, 'aditya', 99.0], [3, 'aksath', 85.0], [4, 'divya', 87.0], [5, 'hemmant',
98.0], [6, 'tanvi', 74.0], [7, 'prajwal', 60.0], [8, 'ritika', 79.0], [9, 'smarthak', 90.0], [10,
'mayank', 75.0]]

enter your choice:- 12


exiting this program....

You might also like