Uploaded by Midun Narayanan. C

Python Binary File Programs: Student Data Management

advertisement
BINARY FILE PROGRAMS IN PYTHON
CLASS-XII COMPUTER SC(2020-21)
Q. WAM TO CREATE A BINARY FILE STUDENT.DAT TO INPUT THE FOLLOWING
DETAILS OF STUDENT(NMAE,CLASS &SEC, MARKS)
import pickle
def cfile( ):#write records of students into file
f=open("student.dat","wb")#opens file in write & binary mode
#name of the student,class and sec,marks
sname=input("enter the student name: ")
cl=input("enter class and sec : ")
mark=int(input("enter the marks: "))
rec= [sname,cl,mark]
pickle.dump(rec,f)
f.close( )
print("file is created")
Q.WAM TO ADD A RECORD IN A BINARY FILE STUDENT.DAT .
import pickle
def appfile( ):
f=open("student.dat","ab") #opens file in append & binary mode
sname=input("enter the student name: ")
cl=input("enter class and sec : ")
mark=int(input("enter the marks: "))
rec= [sname,cl,mark]
pickle.dump(rec,f)
f.close( )
Q.WAM TO READ/DISPLAY ALL RECORDS OF A BINARY FILE STUDENT.DAT .
import pickle
def dispfile( ):
f=open("student.dat","rb")
try:
while True:
b=pickle.load(f)
print(b[0],b[1],b[2])
# print(b)
except EOFError:
f.close()
Q.WAM TO DIPLAY ALL RECORDS WHOSE MARKS IS >=400 FROM ABINARY FILE
STUDENT.DAT .
import pickle
def dispmarks_400( ):
f=open("student.dat","rb")
try:
while True:
b=pickle.load(f)
if b[2]>=400 :
print(b[0],b[1],b[2])
except EOFError:
f.close()
Q.WAM TO CREATE A BINARY FILE STUDENT.DAT TO INPUT THE FOLLOWING
DETAILS OF STUDENT
import pickle
def disptotal( ):
f=open("student.dat","rb")
t=0
try:
while True:
b=pickle.load(f)
t+=b[2]
except EOFError:
f.close()
print('total marks of all the students:',t)
Q.WAM TO SEARCH A GIVEN NAME FROM A GIVEN BINARY FILE STUDENT.DAT.
import pickle
def searchfile( ):
f=open("student.dat","rb")
nm =input("enter name to be searched")
try:
while True:
b=pickle.load(f)
if b[0]==nm:
print(b)
except EOFError:
f.close()
Q. WAM TO COPY ALL RECORDS OF STUDENT.DAT FILE TO S.DAT FILE.
import pickle
def copyfile( ):
f=open("student.dat","rb")
f1=open("s.dat","wb")
try:
while True:
b=pickle.load(f)
pickle.dump(b,f1)
except EOFError:
f.close()
f1.close()
print("file is copied ,sucessfully")
Q. WRITE A MENU DRIVEN PROGRAM IN PYTHON TO DO THE FOLLOWING
OPERATIONS IN BINARY FILE “stu.dat” .
1. Add records
2. Display records
3. Print total marks of students
4. Print student name whose marks >=400
5. To search a given name in a file
6. Exit
import pickle
def cfile( ):
f=open("stu.dat","ab")
sname=input("enter the student name: ")
cl=input("enter class and sec : ")
mark=int(input("enter the marks: "))
rec= [sname,cl,mark]
pickle.dump(rec,f)
f.close( )
def rfile( ):
f=open("stu.dat","rb")
try:
while True:
r=pickle.load(f)
print(r)
except EOFError:
f.close()
def totalmarks( ):
f=open("stu.dat","rb")
t=0
try:
#retreive one student record from file
while True:
r=pickle.load(f)
t=t+r[2]#add marks
except EOFError:
f.close()
print("Total marks of student: ",t)
def dispname( ):
f=open("stu.dat","rb")
t=0
try:
while True:
r=pickle.load(f)
if r[2]>400:
print(r[0],r[1])
#print student name,class
except EOFError:
f.close()
def searchfile( ):
f=open("stu.dat","rb")
nm =input("enter name to be searched")
try:
while True:
r=pickle.load(f)
if r[0]==nm:
print(r)
except EOFError:
f.close()
ch=1
while ch!=6:
print("MAIN MENU")
print("1. Add records")
print("2. Display records")
print("3. Print total marks of students")
print("4. Print student name whose marks >400")
print("5. To search a given name in a file")
print("6. Exit")
ch=int(input("Enter your choice (1-6) : "))
if ch==1:
cfile()
elif ch==2:
rfile()
elif ch==3:
totalmarks()
elif ch==4:
dispname()
elif ch==5:
searchfile()
elif ch!=5:
print("Invalid Choice")
print("Thanks for running the program")
NOTE: IN PDF , MAY BE INDENTATION CHANGED.
Download